From 891566222e24e835bb4a06a33292bfb38ea17f50 Mon Sep 17 00:00:00 2001 From: John Smith Date: Tue, 19 Mar 2024 22:44:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E4=BF=AE=E5=A4=8DChrome?= =?UTF-8?q?=E6=8A=93WS=E4=BA=8C=E8=BF=9B=E5=88=B6=E5=8C=85=E6=97=B6?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E7=A9=BA=E5=8C=85=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/chat/ChatClientDirectWeb.js | 1 + .../api/chat/ChatClientOfficialBase/index.js | 29 +++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/frontend/src/api/chat/ChatClientDirectWeb.js b/frontend/src/api/chat/ChatClientDirectWeb.js index 7883b8a..c8e3f14 100644 --- a/frontend/src/api/chat/ChatClientDirectWeb.js +++ b/frontend/src/api/chat/ChatClientDirectWeb.js @@ -57,6 +57,7 @@ export default class ChatClientDirectWeb extends ChatClientOfficialBase { platform: 'web', type: 2, buvid: '', + // TODO B站开始检查key了,有空时加上 } this.websocket.send(this.makePacket(authParams, base.OP_AUTH)) } diff --git a/frontend/src/api/chat/ChatClientOfficialBase/index.js b/frontend/src/api/chat/ChatClientOfficialBase/index.js index 3903711..eaafebe 100644 --- a/frontend/src/api/chat/ChatClientOfficialBase/index.js +++ b/frontend/src/api/chat/ChatClientOfficialBase/index.js @@ -73,20 +73,25 @@ export default class ChatClientOfficialBase { } makePacket(data, operation) { - let body if (typeof data === 'object') { - body = textEncoder.encode(JSON.stringify(data)) - } else { // string - body = textEncoder.encode(data) + data = JSON.stringify(data) } - let header = new ArrayBuffer(HEADER_SIZE) - let headerView = new DataView(header) - headerView.setUint32(0, HEADER_SIZE + body.byteLength) // pack_len - headerView.setUint16(4, HEADER_SIZE) // raw_header_size - headerView.setUint16(6, 1) // ver - headerView.setUint32(8, operation) // operation - headerView.setUint32(12, 1) // seq_id - return new Blob([header, body]) + let bodyArr = textEncoder.encode(data) + + let headerBuf = new ArrayBuffer(HEADER_SIZE) + let headerView = new DataView(headerBuf) + headerView.setUint32(0, HEADER_SIZE + bodyArr.byteLength) // pack_len + headerView.setUint16(4, HEADER_SIZE) // raw_header_size + headerView.setUint16(6, 1) // ver + 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() {