建设网站公司哪儿济南兴田德润有活动吗,攀枝花移动网站建设,网站设计流程电话,网站建设 康盛设计教程一、Websocket获取B站直播间弹幕教程 — 哔哩哔哩直播开放平台
1、封包
我们连接上B站Websocket成功后#xff0c;要做两件事情#xff1a;
第一、发送鉴权包。第二、发送心跳包#xff0c;每30秒一次#xff0c;维持websocket连接。
这两个包不是直接发送过去要做两件事情
第一、发送鉴权包。第二、发送心跳包每30秒一次维持websocket连接。
这两个包不是直接发送过去而是要创建byte数组将一些数据 按B站协议格式 用大端序写入到byte数组。
协议 1、4 bytePacket Length整个Packet的长度包含Header。2、2 byteHeader LengthHeader的长度固定为16。3、2 byteVersion 如果Version0Body中就是实际发送的数据。如果Version2Body中是经过压缩后的数据请使用zlib解压然后按照Proto协议去解析。 4、4 byteOperation消息的类型 Operation 2客户端发送的心跳包(30秒发送一次)Operation 3 服务器收到心跳包的回复Operation 5 服务器推送的弹幕消息包Operation 7 客户端发送的鉴权包(客户端发送的第一个包)Operation 8 服务器收到鉴权包后的回复 5、4 byteSequence ID保留字段可以忽略。6、? byteBody消息体 body为json格式字符串 -- 转Byte数组如果是心跳包body就为空
例子
这边以JAVA代码为例。 代码依赖了FastJson2用于将JsonMap转Json字符串。
public static byte[] pack(String jsonStr, short code){byte[] contentBytes new byte[0];//如果是鉴权包那一定带有jsonStrif(7 code){contentBytes jsonStr.getBytes();}try(ByteArrayOutputStream data new ByteArrayOutputStream();DataOutputStream stream new DataOutputStream(data)){stream.writeInt(contentBytes.length 16);//封包总大小stream.writeShort(16);//头部长度 header的长度固定为16stream.writeShort(0);//Version, 客户端一般发送的是普通数据。stream.writeInt(code);//操作码封包类型stream.writeInt(1);//保留字段可以忽略。if(7 code){stream.writeBytes(jsonStr);}return data.toByteArray();}
}这样封包的方法就写好了jsonStr为要发的数据code为包的类型。
定义生成鉴权包的方法
public byte[] generateAuthPack(String jsonStr) throws IOException {return pack(jsonStr, 7);
}如果你是非官方开放API接口调用那jsonStr得自己生成。
public byte[] generateAuthPack(String uid, String buvidString token, int roomid){JSONObject jo new JSONObject();jo.put(uid, uid);jo.put(buvid, buvid);jo.put(roomid, roomid);jo.put(protover, 0);jo.put(platform, web);jo.put(type, 2);jo.put(key, token);return pack(jo.toString(), 7);
}参数 uid : 你Cookie的DedeUserIDbuvid : 你Cookie的buvid3token : 鉴于是否登录tokenroomid : 直播间ID
如何获取Token? ---- 【JAVA版本】最新websocket获取B站直播弹幕——非官方API
定义生成心跳包的方法
public static byte[] generateHeartBeatPack() throws IOException {return pack(null, 2);
}
2、解包
成功鉴权后我们获取到B站数据也都是byte数组格式跟上面一样按上面的格式来读取就行了。 不过如果是Zip包那稍微麻烦点。 获得zip包的body后还得进行解压。
先来个流程图 Created with Raphaël 2.3.0 开始解析byte数组 从 下标0 开始读取 4byte 这是包长度body长度header长度。 从 下标4 开始读取 2byte 这是header长度。 从 下标6 开始读取 2byte Version0、普通数据2、zip数据 从 下标8 开始读取 4byte Operation消息的类型。 从 下标12 开始读取 4byte 这是保留字段可以忽略。 判断Version是否为2zip包 解压Zip数组 从下标16到读取到“body长度” body长度 包长度 - header长度 判断是否读完byte数组 结束解析 yes no yes no 代码实现
同样以JAVA代码为例 代码依赖了hutool-core用于zip数组解压。
public static void unpack(ByteBuffer byteBuffer){int packageLen byteBuffer.getInt();short headLength byteBuffer.getShort();short protVer byteBuffer.getShort();int optCode byteBuffer.getInt();int sequence byteBuffer.getInt();if(3 optCode){System.out.println(这是服务器心跳回复);}byte[] contentBytes new byte[packageLen - headLength];byteBuffer.get(contentBytes);//如果是zip包就进行解包if(2 protVer){unpack(ByteBuffer.wrap(ZipUtil.unZlib(contentBytes)));return;}String content new String(contentBytes, StandardCharsets.UTF_8);if(8 optCode){//返回{code:0}表示成功System.out.println(这是鉴权回复content);}//真正的弹幕消息if(5 optCode){System.out.println(真正的弹幕消息content);// todo 自定义处理}//只存在ZIP包解压时才有的情况//如果byteBuffer游标 小于 byteBuffer大小那就证明还有数据if(byteBuffer.position() byteBuffer.limit()){unpack(byteBuffer);}}