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 = () => {