ws连接服务端代码初步打通

This commit is contained in:
bob
2024-09-12 21:38:29 +08:00
parent 7958b7e4d6
commit ed58e3b2f9

View File

@@ -83,7 +83,6 @@ class WsConnect {
const hello = Msg.create({ header: header })
const payload = Msg.encode(hello).finish()
const data = this.encodePayload(payload)
console.log('encode data: ', data)
this.connect.send(data)
}
@@ -110,31 +109,6 @@ class WsConnect {
return Uint8Array.of(...lenEncode, ...payload)
}
/**
* 接收消息后对payload进行解码剥离掉长度
* @param {*} buf
* @returns
*/
decodePayload(buf) {
const view = new DataView(buf)
let byteIndex = 0
let length = 0
let shift = 0
// eslint-disable-next-line no-constant-condition
while (true) {
const byte = view.getUint8(byteIndex)
length |= (byte & 0x7f) << shift
byteIndex++
if ((byte & 0x80) === 0) {
break
}
shift += 7
}
return new Uint8Array(buf, byteIndex, length)
}
/**
* 数据解码,其中解决了半包黏包问题
* @param {*} data