增加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

@ -22,82 +22,98 @@ const chat = {
state.socket = socket; state.socket = socket;
storage.set(constant.socket, socket); storage.set(constant.socket, socket);
}, },
// SET_sysMessageInfo: (state, sysMessageInfo) => {
// state.sysMessageInfo = sysMessageInfo;
// 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) => { SET_userMessageinfo: (state, userMessageinfo) => {
state.userMessageinfo = userMessageinfo; state.userMessageinfo = userMessageinfo;
storage.set(constant.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)) { console.error("WebSocket发生错误", error);
// commit("SET_interactMessage",jsonObject) reconnect();
// } });
if (jsonObject.messageType == 51 || (jsonObject.messageType == 52)) {
commit("SET_userMessageinfo",jsonObject) socket.onClose(() => {
console.log("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;
} }
}; };
socket.onerror = (error) => { const reconnect = () => {
console.error("WebSocket发生错误", error); stopHeartBeat();
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
reconnectTimeout = setTimeout(() => {
console.log("尝试重新连接WebSocket");
connect();
}, 5000); // 5秒后重连
}; };
socket.onclose = () => { connect();
console.log("WebSocket连接已关闭");
reject("WebSocket连接已关闭");
};
});
}); });
}, },
}, },