消息发送-5:消息处理按照MsgType拆分代码块

This commit is contained in:
bob
2024-09-15 18:16:19 +08:00
parent fda668cae2
commit 5481c73f09
4 changed files with 104 additions and 72 deletions

View File

@@ -0,0 +1,74 @@
import { Msg, Header, MsgType, Body } from '@/proto/msg'
import { proto } from '@/const/msgConst'
import { userStore } from '@/stores'
import { v4 as uuidv4 } from 'uuid'
export const chatConstructor = (toId, content) => {
const header = Header.create({
magic: proto.magic,
version: proto.version,
msgType: MsgType.CHAT,
isExtension: false
})
const userData = userStore()
const body = Body.create({
fromId: userData.user.account,
fromClient: userData.clientId,
toId: toId,
content: content,
tempMsgId: uuidv4()
})
const chatMsg = Msg.create({ header: header, body: body })
const payload = Msg.encode(chatMsg).finish()
const data = encodePayload(payload)
return data
}
export const heartBeatConstructor = () => {
const header = Header.create({
magic: proto.magic,
version: proto.version,
msgType: MsgType.HEART_BEAT,
isExtension: false
})
const heartBeat = Msg.create({ header: header })
const payload = Msg.encode(heartBeat).finish()
const data = encodePayload(payload)
return data
}
export const helloConstructor = () => {
const header = Header.create({
magic: proto.magic,
version: proto.version,
msgType: MsgType.HELLO,
isExtension: false
})
const hello = Msg.create({ header: header })
const payload = Msg.encode(hello).finish()
const data = encodePayload(payload)
return data
}
/**
* 发送前对长度编码,配合服务端解决半包黏包问题
* @param {*} payload
* @returns
*/
const encodePayload = (payload) => {
let num = payload.length
let lenEncode = []
while (num > 0) {
let byte = num & 0x7f
num >>= 7
if (num > 0) {
byte |= 0x80
}
lenEncode.push(byte)
}
return Uint8Array.of(...lenEncode, ...payload)
}