消息发送-3:发送消息是绑定delivered事件,拿到服务端生成的msgid

This commit is contained in:
bob
2024-09-15 10:45:41 +08:00
parent 23d9e7f52f
commit 767981bc46
2 changed files with 34 additions and 10 deletions

View File

@@ -73,6 +73,13 @@ class WsConnect {
curReSendTimes: 0 // 当前重发的次数
}
/**
* 绑定的业务事件
*/
events = {
delivered: () => {}
}
/**
* 构造函数
*/
@@ -160,6 +167,7 @@ class WsConnect {
break
case MsgType.DELIVERED:
console.log('receive a DELIVERED message, it is: ', msg)
this.handleDelivered(msg)
break
case MsgType.SENDER_SYNC:
break
@@ -222,6 +230,10 @@ class WsConnect {
})
}
handleDelivered(msg) {
this.events.delivered(msg)
}
/**
* 发送前对长度编码,配合服务端解决半包黏包问题
* @param {*} payload
@@ -293,12 +305,21 @@ class WsConnect {
return newBuffer
}
/**
* 绑定事件
* @param {*} event
* @param {*} callback
*/
bindEvent(event, callback) {
this.events[event] = callback
}
/**
* 发送msg封装了重发机制 TODO 这个函数要按照消息类型拆出来多个
* @param {*} toId
* @param {*} msgType
* @param {*} content
* @param {*} callback 失败回调
* @param {*} callback
*/
sendMsg(toId, msgType, content, callback) {
const header = Header.create({
@@ -323,12 +344,13 @@ class WsConnect {
const data = this.encodePayload(payload)
if (this.isConnect) {
this.bindEvent('delivered', callback)
this.connect.send(data)
} else {
if (this.reSend.curReSendTimes >= this.reSend.timeoutTimes) {
console.log('resend to0 many times')
console.log('resend too many times')
this.reSend.curReSendTimes = 0
callback()
// TODO 应该反馈到业务层给提示
} else {
setTimeout(() => {
this.sendMsg(data, callback)

View File

@@ -135,14 +135,16 @@ const handleExportData = (data) => {
}
// 发送事件要做的事情
const handleExportContent = (content) => {
wsConnect.sendMsg(curObject.value.account, MsgType.CHAT, content, () => {})
messageData.addMsgRecord(curSessionId.value, {
msgId: 0, // TODO 这个msgid在发出去的时候没有收到“已发送”消息才有的根据tempMsgId反填的
fromId: userData.user.account,
msgType: MsgType.CHAT, // TODO 这里应该是读取sessionType但是sessionType没有按照MsgType写
msgTime: new Date(),
content: content
wsConnect.sendMsg(curObject.value.account, MsgType.CHAT, content, (deliveredMsg) => {
messageData.addMsgRecord(curSessionId.value, {
msgId: deliveredMsg.body.msgId,
fromId: userData.user.account,
msgType: MsgType.CHAT, // TODO 这里应该是读取sessionType但是sessionType没有按照MsgType写
msgTime: new Date(),
content: content
})
})
}
const onLoadMore = () => {