前端修复Chrome抓WS二进制包时显示空包的问题

This commit is contained in:
John Smith 2024-03-19 22:44:21 +08:00
parent ae248d0d70
commit 891566222e
2 changed files with 18 additions and 12 deletions

View File

@ -57,6 +57,7 @@ export default class ChatClientDirectWeb extends ChatClientOfficialBase {
platform: 'web', platform: 'web',
type: 2, type: 2,
buvid: '', buvid: '',
// TODO B站开始检查key了有空时加上
} }
this.websocket.send(this.makePacket(authParams, base.OP_AUTH)) this.websocket.send(this.makePacket(authParams, base.OP_AUTH))
} }

View File

@ -73,20 +73,25 @@ export default class ChatClientOfficialBase {
} }
makePacket(data, operation) { makePacket(data, operation) {
let body
if (typeof data === 'object') { if (typeof data === 'object') {
body = textEncoder.encode(JSON.stringify(data)) data = JSON.stringify(data)
} else { // string
body = textEncoder.encode(data)
} }
let header = new ArrayBuffer(HEADER_SIZE) let bodyArr = textEncoder.encode(data)
let headerView = new DataView(header)
headerView.setUint32(0, HEADER_SIZE + body.byteLength) // pack_len let headerBuf = new ArrayBuffer(HEADER_SIZE)
headerView.setUint16(4, HEADER_SIZE) // raw_header_size let headerView = new DataView(headerBuf)
headerView.setUint16(6, 1) // ver headerView.setUint32(0, HEADER_SIZE + bodyArr.byteLength) // pack_len
headerView.setUint32(8, operation) // operation headerView.setUint16(4, HEADER_SIZE) // raw_header_size
headerView.setUint32(12, 1) // seq_id headerView.setUint16(6, 1) // ver
return new Blob([header, body]) headerView.setUint32(8, operation) // operation
headerView.setUint32(12, 1) // seq_id
// 这里如果直接返回 new Blob([headerBuf, bodyArr])在Chrome抓包会显示空包实际上是有数据的为了调试体验最好还是拷贝一遍
let headerArr = new Uint8Array(headerBuf)
let packetArr = new Uint8Array(bodyArr.length + headerArr.length)
packetArr.set(headerArr)
packetArr.set(bodyArr, headerArr.length)
return packetArr
} }
sendAuth() { sendAuth() {