Przeglądaj źródła

fix:修复断线重连时可能会出现消息乱序的bug

xsx 2 lat temu
rodzic
commit
f18a8e62eb
2 zmienionych plików z 20 dodań i 4 usunięć
  1. 10 2
      im-ui/src/store/chatStore.js
  2. 10 2
      im-uniapp/store/chatStore.js

+ 10 - 2
im-ui/src/store/chatStore.js

@@ -174,8 +174,16 @@ export default {
 				});
 				chat.lastTimeTip = msgInfo.sendTime;
 			}
-			// 新的消息
-			chat.messages.push(msgInfo);
+			// 根据id顺序插入,防止消息乱序
+			let insertPos = chat.messages.length;
+			for (let idx in chat.messages) {
+				if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) {
+					insertPos = idx;
+					console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`);
+					break;
+				}
+			}
+			chat.messages.splice(insertPos, 0, msgInfo);
 			this.commit("saveToStorage");
 		},
 		updateMessage(state, msgInfo) {

+ 10 - 2
im-uniapp/store/chatStore.js

@@ -176,8 +176,16 @@ export default {
 				});
 				chat.lastTimeTip = msgInfo.sendTime;
 			}
-			// 新的消息
-			chat.messages.push(msgInfo);
+			// 根据id顺序插入,防止消息乱序
+			let insertPos = chat.messages.length;
+			for (let idx in chat.messages) {
+				if (chat.messages[idx].id && msgInfo.id < chat.messages[idx].id) {
+					insertPos = idx;
+					console.log(`消息出现乱序,位置:${chat.messages.length},修正至:${insertPos}`);
+					break;
+				}
+			}
+			chat.messages.splice(insertPos, 0, msgInfo);
 			this.commit("saveToStorage");
 		},
 		updateMessage(state, msgInfo) {