From 767981bc46a4623fd53af2fd62973d537da50b8f Mon Sep 17 00:00:00 2001 From: bob <312777916@qq.com> Date: Sun, 15 Sep 2024 10:45:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=8F=91=E9=80=81-3=EF=BC=9A?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E6=98=AF=E7=BB=91=E5=AE=9A?= =?UTF-8?q?delivered=E4=BA=8B=E4=BB=B6=EF=BC=8C=E6=8B=BF=E5=88=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF=E7=94=9F=E6=88=90=E7=9A=84msgid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/wsConnect.js | 28 +++++++++++++++++++++++++--- src/views/message/MessageLayout.vue | 16 +++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/api/wsConnect.js b/src/api/wsConnect.js index 871bf3d..e1374e0 100644 --- a/src/api/wsConnect.js +++ b/src/api/wsConnect.js @@ -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) diff --git a/src/views/message/MessageLayout.vue b/src/views/message/MessageLayout.vue index f2c4653..5cc30c1 100644 --- a/src/views/message/MessageLayout.vue +++ b/src/views/message/MessageLayout.vue @@ -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 = () => {