feat(微信/websocket): 新增消息处理逻辑并优化websocket连接
- 在WeChatState接口中添加addMessage方法用于接收新消息 - 在websocket消息处理器中调用addMessage存储接收到的消息 - 优化websocket连接逻辑,断开时自动尝试重连 - 改进聊天窗口视频状态检测逻辑,减少不必要的滚动
This commit is contained in:
@@ -43,10 +43,10 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
|
||||
useEffect(() => {
|
||||
const prevMessages = prevMessagesRef.current;
|
||||
|
||||
// 检查是否有视频状态变化(从加载中变为已完成)
|
||||
// 检查是否有视频状态变化(从加载中变为已完成或开始加载)
|
||||
const hasVideoStateChange = currentMessages.some((msg, index) => {
|
||||
const prevMsg = prevMessages[index];
|
||||
if (!prevMsg) return false;
|
||||
if (!prevMsg || prevMsg.id !== msg.id) return false;
|
||||
|
||||
try {
|
||||
const currentContent =
|
||||
@@ -58,37 +58,33 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
|
||||
? JSON.parse(prevMsg.content)
|
||||
: prevMsg.content;
|
||||
|
||||
// 检查是否从加载中变为已完成(有videoUrl)
|
||||
return (
|
||||
prevContent.isLoading === true &&
|
||||
currentContent.isLoading === false &&
|
||||
currentContent.videoUrl
|
||||
);
|
||||
// 检查视频状态是否发生变化(开始加载、完成加载、获得URL)
|
||||
const currentHasVideo =
|
||||
currentContent.previewImage && currentContent.tencentUrl;
|
||||
const prevHasVideo = prevContent.previewImage && prevContent.tencentUrl;
|
||||
|
||||
if (currentHasVideo && prevHasVideo) {
|
||||
// 检查加载状态变化或视频URL变化
|
||||
return (
|
||||
currentContent.isLoading !== prevContent.isLoading ||
|
||||
currentContent.videoUrl !== prevContent.videoUrl
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
// 检查是否有视频正在加载中
|
||||
const hasLoadingVideo = currentMessages.some(msg => {
|
||||
try {
|
||||
const content =
|
||||
typeof msg.content === "string"
|
||||
? JSON.parse(msg.content)
|
||||
: msg.content;
|
||||
return content.isLoading === true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
// 只有在没有视频加载且没有视频状态变化时才自动滚动到底部
|
||||
if (!hasLoadingVideo && !hasVideoStateChange) {
|
||||
// 只有在没有视频状态变化时才自动滚动到底部
|
||||
if (!hasVideoStateChange) {
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
// 更新上一次的消息状态
|
||||
prevMessagesRef.current = currentMessages;
|
||||
console.log("视频状态变化:", hasVideoStateChange, currentMessages);
|
||||
}, [currentMessages]);
|
||||
|
||||
const scrollToBottom = () => {
|
||||
|
||||
@@ -17,4 +17,5 @@ export interface WeChatState {
|
||||
// 视频消息处理方法
|
||||
setVideoLoading: (messageId: number, isLoading: boolean) => void;
|
||||
setVideoUrl: (messageId: number, videoUrl: string) => void;
|
||||
addMessage: (message: ChatRecord) => void;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import { Messages } from "./msg.data";
|
||||
import { useWeChatStore } from "@/store/module/weChat/weChat";
|
||||
// 消息处理器类型定义
|
||||
type MessageHandler = (message: WebSocketMessage) => void;
|
||||
|
||||
const setVideoUrl = useWeChatStore.getState().setVideoUrl;
|
||||
const addMessage = useWeChatStore.getState().addMessage;
|
||||
// 消息处理器映射
|
||||
const messageHandlers: Record<string, MessageHandler> = {
|
||||
// 微信账号存活状态响应
|
||||
@@ -35,6 +36,7 @@ const messageHandlers: Record<string, MessageHandler> = {
|
||||
//收到消息
|
||||
CmdNewMessage: (message: Messages) => {
|
||||
console.log("收到消息", message.friendMessage);
|
||||
addMessage(message.friendMessage);
|
||||
// 在这里添加具体的处理逻辑
|
||||
},
|
||||
|
||||
@@ -56,7 +58,7 @@ const messageHandlers: Record<string, MessageHandler> = {
|
||||
|
||||
CmdDownloadVideoResult: message => {
|
||||
// 在这里添加具体的处理逻辑
|
||||
useWeChatStore.getState().setVideoUrl(message.friendMessageId, message.url);
|
||||
setVideoUrl(message.friendMessageId, message.url);
|
||||
},
|
||||
// 可以继续添加更多处理器...
|
||||
};
|
||||
|
||||
@@ -231,7 +231,16 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
currentState.status !== WebSocketStatus.CONNECTED ||
|
||||
!currentState.ws
|
||||
) {
|
||||
Toast.show({ content: "WebSocket未连接", position: "top" });
|
||||
Toast.show({
|
||||
content: "WebSocket未连接,正在重新连接...",
|
||||
position: "top",
|
||||
});
|
||||
|
||||
// 重置连接状态并发起重新连接
|
||||
set({ status: WebSocketStatus.DISCONNECTED });
|
||||
if (currentState.config) {
|
||||
currentState.connect(currentState.config);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -247,6 +256,12 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
} catch (error) {
|
||||
// console.error("命令发送失败:", error);
|
||||
Toast.show({ content: "命令发送失败", position: "top" });
|
||||
|
||||
// 发送失败时也尝试重新连接
|
||||
set({ status: WebSocketStatus.DISCONNECTED });
|
||||
if (currentState.config) {
|
||||
currentState.connect(currentState.config);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user