增加ws心跳

dev
26947 2024-08-17 08:56:16 +08:00
parent c7e0cd8803
commit 0768d913ba
3 changed files with 88 additions and 70 deletions

View File

@ -345,7 +345,7 @@
} }
); );
onShow(() => { onShow(() => {
proxy.$store.dispatch('webSockerInfo') //proxy.$store.dispatch('webSockerInfo')
}) })
@ -354,12 +354,12 @@
webInfo2() webInfo2()
} }
const webInfo2 = () => { const webInfo2 = () => {
proxy.$store.dispatch('webSockerInfo') //proxy.$store.dispatch('webSockerInfo')
proxy.$store.dispatch('getQNDomainInfo') proxy.$store.dispatch('getQNDomainInfo')
console.log("操作web2"); console.log("操作web2");
} }
onLoad(() => { onLoad(() => {
proxy.$store.dispatch('webSockerInfo') //proxy.$store.dispatch('webSockerInfo')
GetList(); GetList();
}) })

View File

@ -104,9 +104,11 @@ const grid1Click1 = (e) => {
console.log(e); console.log(e);
} }
onLoad(() => { onLoad(() => {
proxy.$store.dispatch('webSockerInfo')
getCountInfo(); getCountInfo();
}) })
onShow(() => { onShow(() => {
proxy.$store.dispatch('webSockerInfo')
getCountInfo(); getCountInfo();
}) })
const getCountInfo = () =>{ const getCountInfo = () =>{

View File

@ -10,11 +10,11 @@ const chat = {
state: { state: {
token: getToken(), token: getToken(),
socket: storage.get(constant.socket), socket: storage.get(constant.socket),
sysMessageInfo:storage.get(constant.sysMessageInfo), sysMessageInfo: storage.get(constant.sysMessageInfo),
activiInfo:storage.get(constant.activiInfo), activiInfo: storage.get(constant.activiInfo),
sysAuditInfo:storage.get(constant.sysAuditInfo), sysAuditInfo: storage.get(constant.sysAuditInfo),
userMessageinfo:storage.get(constant.userMessageinfo), userMessageinfo: storage.get(constant.userMessageinfo),
interactMessage:storage.get(constant.interactMessage), interactMessage: storage.get(constant.interactMessage),
}, },
mutations: { mutations: {
@ -22,82 +22,98 @@ const chat = {
state.socket = socket; state.socket = socket;
storage.set(constant.socket, socket); storage.set(constant.socket, socket);
}, },
SET_userMessageinfo: (state, userMessageinfo) => {
// SET_sysMessageInfo: (state, sysMessageInfo) => { state.userMessageinfo = userMessageinfo;
// state.sysMessageInfo = sysMessageInfo; storage.set(constant.userMessageinfo, userMessageinfo);
// storage.set(constant.sysMessageInfo, sysMessageInfo); },
// },
// SET_sysActiviInfo: (state, activiInfo) => {
// state.activiInfo = activiInfo;
// storage.set(constant.activiInfo, activiInfo);
// },
// SET_sysAuditInfo: (state, sysAuditInfo) => {
// state.sysAuditInfo = sysAuditInfo;
// storage.set(constant.sysAuditInfo, sysAuditInfo);
// },
SET_userMessageinfo: (state, userMessageinfo) => {
state.userMessageinfo = userMessageinfo;
storage.set(constant.userMessageinfo, userMessageinfo);
},
// SET_interactMessage: (state, interactMessage) => {
// state.interactMessage = interactMessage;
// storage.set(constant.interactMessage, interactMessage);
// }
}, },
actions: { actions: {
// 进行webSocker链接 // 进行webSocket链接
webSockerInfo({ commit }) { webSockerInfo({ commit, state }) {
GetListid().then((res) => { GetListid().then((res) => {
let socket = null; let socket = null;
return new Promise((resolve, reject) => { let heartBeatInterval = null;
const fileWithoutProtocol = baseUrl.replace( let reconnectTimeout = null;
/^https?:\/\//,
""
);
const url = "ws://" + fileWithoutProtocol + "/text/text?" + res;
socket = new WebSocket(url);
socket.onopen = () => { const connect = () => {
commit("SET_socket",socket) const fileWithoutProtocol = baseUrl.replace(/^https?:\/\//, "");
resolve(); const url2 = "ws://" + fileWithoutProtocol + "/text/text?" + res;
socket = uni.connectSocket({
url: url2,
success: () => {
console.log("ws连接成功");
},
});
commit("SET_socket", socket);
socket.onOpen(() => {
console.log("WebSocket连接已打开"); console.log("WebSocket连接已打开");
}; // 开始心跳
startHeartBeat();
resolve();
});
socket.onmessage = (event) => { socket.onMessage((event) => {
console.log("收到WebSocket消息", event.data); console.log("收到WebSocket消息", event.data);
const jsonObject = JSON.parse(event.data); const jsonObject = JSON.parse(event.data);
// if (jsonObject.messageType == 1){ if (jsonObject.messageType == 51 || jsonObject.messageType == 52) {
// console.log("系统通知:",jsonObject); console.log("聊天新消息111:", jsonObject);
// commit("SET_sysMessageInfo",jsonObject.messageText) commit("SET_userMessageinfo", jsonObject);
// } }
// else if (jsonObject.messageType == 2){ });
// console.log("审核通知111:",jsonObject);
// commit("SET_sysAuditInfo",jsonObject.messageText)
// }
// else if (jsonObject.messageType == 51 || (jsonObject.messageType == 52)) {
// commit("SET_userMessageinfo",jsonObject) socket.onError((error) => {
// }else if (jsonObject.messageType == 61 || (jsonObject.messageType == 62)) {
// commit("SET_interactMessage",jsonObject)
// }
if (jsonObject.messageType == 51 || (jsonObject.messageType == 52)) {
commit("SET_userMessageinfo",jsonObject)
}
};
socket.onerror = (error) => {
console.error("WebSocket发生错误", error); console.error("WebSocket发生错误", error);
}; reconnect();
});
socket.onclose = () => { socket.onClose(() => {
console.log("WebSocket连接已关闭"); console.log("WebSocket连接已关闭");
reject("WebSocket连接已关闭"); 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();
}); });
}, },
}, },