mirror of
https://gitee.com/lijingbo-2021/open-anylink-web.git
synced 2025-12-30 11:02:25 +00:00
ws连接服务端代码初步打通
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user