talk_appAmin/store/modules/chat.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-06-29 18:35:54 +08:00
import config from "@/config";
import storage from "@/utils/storage";
import constant from "@/utils/constant";
import { login, logout, getInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { GetListid } from "@/api/signln/SignTa/SignTa";
const baseUrl = config.baseUrl;
const qnDomain = config.qnDomain;
const chat = {
state: {
token: getToken(),
socket: storage.get(constant.socket),
2024-08-17 08:56:16 +08:00
sysMessageInfo: storage.get(constant.sysMessageInfo),
activiInfo: storage.get(constant.activiInfo),
sysAuditInfo: storage.get(constant.sysAuditInfo),
userMessageinfo: storage.get(constant.userMessageinfo),
interactMessage: storage.get(constant.interactMessage),
2024-06-29 18:35:54 +08:00
},
mutations: {
SET_socket: (state, socket) => {
state.socket = socket;
storage.set(constant.socket, socket);
},
2024-08-17 08:56:16 +08:00
SET_userMessageinfo: (state, userMessageinfo) => {
state.userMessageinfo = userMessageinfo;
storage.set(constant.userMessageinfo, userMessageinfo);
},
2024-06-29 18:35:54 +08:00
},
actions: {
2024-08-17 08:56:16 +08:00
// 进行webSocket链接
webSockerInfo({ commit, state }) {
2024-06-29 18:35:54 +08:00
GetListid().then((res) => {
let socket = null;
2024-08-17 08:56:16 +08:00
let heartBeatInterval = null;
let reconnectTimeout = null;
const connect = () => {
const fileWithoutProtocol = baseUrl.replace(/^https?:\/\//, "");
const url2 = "ws://" + fileWithoutProtocol + "/text/text?" + res;
socket = uni.connectSocket({
url: url2,
success: () => {
console.log("ws连接成功");
},
});
commit("SET_socket", socket);
2024-06-29 18:35:54 +08:00
2024-08-17 08:56:16 +08:00
socket.onOpen(() => {
console.log("WebSocket连接已打开");
// 开始心跳
startHeartBeat();
2024-06-29 18:35:54 +08:00
resolve();
2024-08-17 08:56:16 +08:00
});
2024-06-29 18:35:54 +08:00
2024-08-17 08:56:16 +08:00
socket.onMessage((event) => {
2024-06-29 18:35:54 +08:00
console.log("收到WebSocket消息", event.data);
const jsonObject = JSON.parse(event.data);
2024-08-17 08:56:16 +08:00
if (jsonObject.messageType == 51 || jsonObject.messageType == 52) {
console.log("聊天新消息111:", jsonObject);
commit("SET_userMessageinfo", jsonObject);
}
});
2024-06-29 18:35:54 +08:00
2024-08-17 08:56:16 +08:00
socket.onError((error) => {
2024-06-29 18:35:54 +08:00
console.error("WebSocket发生错误", error);
2024-08-17 08:56:16 +08:00
reconnect();
});
2024-06-29 18:35:54 +08:00
2024-08-17 08:56:16 +08:00
socket.onClose(() => {
2024-06-29 18:35:54 +08:00
console.log("WebSocket连接已关闭");
2024-08-17 08:56:16 +08:00
reconnect();
});
};
const startHeartBeat = () => {
heartBeatInterval = setInterval(() => {
if (socket && socket.readyState === 1) {
const info = {
fromid: null,
toid: 0,
content: "heartBeat",
};
socket.send({
data: JSON.stringify(info),
success: () => {
console.log("心跳包发送成功");
},
});
}
}, 5000); // 每5秒发送一次心跳包
};
const stopHeartBeat = () => {
if (heartBeatInterval) {
clearInterval(heartBeatInterval);
heartBeatInterval = null;
}
};
const reconnect = () => {
stopHeartBeat();
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
reconnectTimeout = setTimeout(() => {
console.log("尝试重新连接WebSocket");
connect();
}, 5000); // 5秒后重连
};
connect();
2024-06-29 18:35:54 +08:00
});
},
},
};
export default chat;