第二个问题,因为 web 端本地是不持久化存储消息的,所以收到消息时,会把上次收到消息,到这次收到消息之间的消息都拉取,这可能会包含自己发送的消息。
demo 在上层收到消息的监听里面,会判断本地是否已存在,如果已存在的话,对消息进行更新;否则插入。
wfc.eventEmitter.on(EventType.ReceiveMessage, (msg, hasMore) => {
//...
// 会把下来加载更多加载的历史消息给清理了
let lastTimestamp = 0;
let msgListLength = conversationState.currentConversationMessageList.length;
if (msgListLength > 0) {
lastTimestamp = conversationState.currentConversationMessageList[msgListLength - 1].timestamp;
}
this._patchMessage(msg, lastTimestamp);
let msgIndex = conversationState.currentConversationMessageList.findIndex(m => {
return m.messageId === msg.messageId
|| (gt(m.messageUid, 0) && eq(m.messageUid, msg.messageUid))
|| (m.messageContent.type === MessageContentType.Streaming_Text_Generating
&& (msg.messageContent.type === MessageContentType.Streaming_Text_Generating || msg.messageContent.type === MessageContentType.Streaming_Text_Generated)
&& m.messageContent.streamId === msg.messageContent.streamId
)
});
if (msgIndex > -1) {
// FYI: https://v2.vuejs.org/v2/guide/reactivity#Change-Detection-Caveats
conversationState.currentConversationMessageList.splice(msgIndex, 1, msg);
console.log('msg duplicate, update message')
return;
} else {
let firstMsg = conversationState.currentConversationMessageList[0];
if(firstMsg && lt(msg.timestamp, firstMsg.timestamp)) {
console.log('msg timestamp is less than first msg, maybe update old message content, ignore')
return;
}
}
//...