2024-04-04 19:28:18 +08:00
|
|
|
|
import signalR from "@/utils/signalR";
|
|
|
|
|
|
import useChatStore from "@/stores/chat";
|
|
|
|
|
|
|
|
|
|
|
|
const receiveMsg = (connection) => {
|
|
|
|
|
|
const chatStore = useChatStore();
|
|
|
|
|
|
//上线用户
|
|
|
|
|
|
connection.on("liveUser", (user) => {
|
|
|
|
|
|
chatStore.addUser(user);
|
|
|
|
|
|
});
|
|
|
|
|
|
//下线用户
|
|
|
|
|
|
connection.on("offlineUser", (userId) => {
|
|
|
|
|
|
chatStore.delUser(userId);
|
|
|
|
|
|
});
|
|
|
|
|
|
//接受其他用户消息
|
2025-02-03 01:18:15 +08:00
|
|
|
|
connection.on("receiveMsg", (type,relStr, content) => {
|
2024-07-21 13:37:56 +08:00
|
|
|
|
const letChatStore = useChatStore();
|
|
|
|
|
|
//如果是ai消息,还要进行流式显示
|
2025-02-03 01:18:15 +08:00
|
|
|
|
if (type === 3) {//(ai消息,还需要支持动态类型)
|
|
|
|
|
|
content.messageType ='ai@'+ relStr;
|
2024-07-21 13:37:56 +08:00
|
|
|
|
letChatStore.addOrUpdateMsg(content);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
letChatStore.addMsg(content);
|
|
|
|
|
|
}
|
2024-04-04 19:28:18 +08:00
|
|
|
|
});
|
|
|
|
|
|
//用户状态-正在输入中,无
|
|
|
|
|
|
connection.on("userStatus", (type) => {
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
2024-07-21 13:37:56 +08:00
|
|
|
|
export function start() {
|
2024-04-04 19:28:18 +08:00
|
|
|
|
signalR.start(`chat`, receiveMsg);
|
|
|
|
|
|
}
|
2024-07-21 13:37:56 +08:00
|
|
|
|
export function close() {
|
2024-04-07 16:35:21 +08:00
|
|
|
|
signalR.SR.stop();
|
|
|
|
|
|
}
|
2024-04-04 19:28:18 +08:00
|
|
|
|
|