1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 1、状态结构设计: chats: { byConversationId: { "uid_123": { messages: [msg1, msg2, ...], unreadCount: 5, lastMessage: {...} } }, currentChatId: "uid_123" }
2、特点 & 方案: -消息按会话分组(byId); -接收消息由 WebSocket middleware 注入; -切换会话时,清空该会话未读数; -输入草稿也可保存在 state(避免切换丢失)。
3、示例处理逻辑: onSocketMessage(message) { const { from, to, content } = message; dispatch({ type: 'RECEIVE_MESSAGE', payload: message });
if (from !== currentChatId) { dispatch({ type: 'INCREASE_UNREAD', payload: from }); } }
4、已读回执: 在进入 chat 页面时触发 dispatch(readMessages(conversationId)), 并通过 WebSocket 告知对方。
|