本文主要根据哔哩哔哩官网player-loader.js内的具体实现进行解析说明。

  • WebSocket Request URL: wss://hw-bj-live-comet-02.chat.bilibili.com/sub
  • 房间初ID始化URL: https://api.live.bilibili.com/room/v1/Room/room_init?id=roomId

封包格式

key bytes offset value name
headerLen 2 4 16 Header Length
ver 2 6 1 Protocol Version
op 4 8 1 Operation
seq 4 12 1 Sequence Id

代码示例:

var wsBinaryHeaderList = [{
  name: "Header Length",
  key: "headerLen",
  bytes: 2,
  offset: 4,
  value: 16
}, {
  name: "Protocol Version",
  key: "ver",
  bytes: 2,
  offset: 6,
  value: 1
}, {
  name: "Operation",
  key: "op",
  bytes: 4,
  offset: 8,
  value: 1
}, {
  name: "Sequence Id",
  key: "seq",
  bytes: 4,
  offset: 12,
  value: 1
}];

操作码

说明
2 客户端发送的心跳包
3 心跳包回复
5 消息
7 认证加入房间
8 服务端发送的心跳包,客户端接收到此信息时需要返回一个心跳包

加入房间认证参数

var auth_params = {
  'uid': 1, // 0为未登录用户
  'roomid': 5050,
  'protover': 1,
  'platform': 'web',
  'clientver': '1.4.0'
};

查看完整代码

参考

bilibili/player-loader.js