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?:\/\//, "");
|
2024-11-06 15:50:08 +08:00
|
|
|
|
const url2 = "ws://" + fileWithoutProtocol + "/text/text?" + getToken();
|
2024-08-17 08:56:16 +08:00
|
|
|
|
|
2024-10-09 20:09:05 +08:00
|
|
|
|
// const url2 = "ws://www.daxueshen.top/text/text?" + res;
|
|
|
|
|
socket = uni.connectSocket({
|
|
|
|
|
url: url2,
|
|
|
|
|
success: () => {
|
|
|
|
|
console.log("ws连接成功");
|
|
|
|
|
},
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
console.error("ws连接失败:", err);
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-08-17 08:56:16 +08:00
|
|
|
|
|
|
|
|
|
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-10-09 20:09:05 +08:00
|
|
|
|
|
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
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-10-10 21:58:52 +08:00
|
|
|
|
let heartbeatIntervalTime = 5000; // 初始心跳间隔时间(毫秒)
|
|
|
|
|
const maxHeartbeatInterval = 30000; // 最大心跳间隔时间(毫秒),例如设置为30秒
|
2024-08-17 08:56:16 +08:00
|
|
|
|
const startHeartBeat = () => {
|
|
|
|
|
heartBeatInterval = setInterval(() => {
|
|
|
|
|
if (socket && socket.readyState === 1) {
|
|
|
|
|
const info = {
|
|
|
|
|
fromid: null,
|
|
|
|
|
toid: 0,
|
|
|
|
|
content: "heartBeat",
|
|
|
|
|
};
|
|
|
|
|
socket.send({
|
|
|
|
|
data: JSON.stringify(info),
|
|
|
|
|
success: () => {
|
2024-10-10 21:58:52 +08:00
|
|
|
|
console.log("心跳包发送成功:",heartbeatIntervalTime);
|
|
|
|
|
// 如果当前心跳间隔小于最大值,则逐步增加心跳间隔
|
|
|
|
|
if (heartbeatIntervalTime < maxHeartbeatInterval) {
|
|
|
|
|
heartbeatIntervalTime += 1000; // 每次增加5秒
|
|
|
|
|
clearInterval(heartBeatInterval); // 清除旧的定时器
|
|
|
|
|
// 开始心跳
|
|
|
|
|
startHeartBeat();
|
|
|
|
|
}
|
2024-08-17 08:56:16 +08:00
|
|
|
|
},
|
2024-10-09 20:09:05 +08:00
|
|
|
|
fail: ()=>{
|
|
|
|
|
console.log("心跳包发送失败,尝试重新连接WebSocket");
|
|
|
|
|
reconnect();
|
|
|
|
|
}
|
2024-08-17 08:56:16 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2024-10-10 21:58:52 +08:00
|
|
|
|
}, heartbeatIntervalTime); // 每5秒发送一次心跳包
|
2024-08-17 08:56:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|