增加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(() => {
proxy.$store.dispatch('webSockerInfo')
//proxy.$store.dispatch('webSockerInfo')
})
@ -354,12 +354,12 @@
webInfo2()
}
const webInfo2 = () => {
proxy.$store.dispatch('webSockerInfo')
//proxy.$store.dispatch('webSockerInfo')
proxy.$store.dispatch('getQNDomainInfo')
console.log("操作web2");
}
onLoad(() => {
proxy.$store.dispatch('webSockerInfo')
//proxy.$store.dispatch('webSockerInfo')
GetList();
})

View File

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

View File

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