挺简单的
let {brotliDecompressSync} = require("zlib"); 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}]; function mergeArrayBuffer(t, e) { var n = new Uint8Array(t) , o = new Uint8Array(e) , i = new Uint8Array(n.byteLength + o.byteLength); return i.set(n, 0), i.set(o, n.byteLength), i.buffer } function convertToArrayBuffer(t, e) { if (e != 2) { t = JSON.stringify(t); } this.encoder = new TextEncoder(); var n = new ArrayBuffer(16) , i = new DataView(n, 0) , s = this.encoder.encode(t); return i.setInt32(0, 16 + s.byteLength), wsBinaryHeaderList[2].value = e, wsBinaryHeaderList.forEach(function (t) { 4 === t.bytes ? i.setInt32(t.offset, t.value) : 2 === t.bytes && i.setInt16(t.offset, t.value) }), [...new Uint8Array(mergeArrayBuffer(n, s))]; } function convertToObject(t) { t = new Uint8Array(t).buffer; var e = new DataView(t) , n = { body: [] }; if (n.packetLen = e.getInt32(0), wsBinaryHeaderList.forEach(function (t) { 4 === t.bytes ? n[t.key] = e.getInt32(t.offset) : 2 === t.bytes && (n[t.key] = e.getInt16(t.offset)) }), n.packetLen < t.byteLength && convertToObject(t.slice(0, n.packetLen)), this.decoder = new TextDecoder(), !n.op || 5 !== n.op && n.op !== 8) n.op && 3 === n.op && (n.body = { count: e.getInt32(16) }); else for (var i = 0, s = n.packetLen, a = "", u = ""; i < t.byteLength; i += s) { s = e.getInt32(i), a = e.getInt16(i + 4); if (n.ver === 0) { var c = this.decoder.decode(t.slice(i + a, i + s)); u = 0 !== c.length ? JSON.parse(c) : null } else if (n.ver === 3) { var l = t.slice(i + a, i + s) , h = new Int8Array(brotliDecompressSync(l)); u = convertToObject(h.buffer).body } u && n.body.push(u) } return n }View Code
挺简单的
标签:直播间,bytes,value,爬取,ws,&&,offset,var,new From: https://www.cnblogs.com/inkser/p/17826504.html