From e77ed6588324c6d5c0943f335aecbd53e1d3fb15 Mon Sep 17 00:00:00 2001 From: zhangzhen <525765282@qq.com> Date: Tue, 2 Dec 2025 11:19:23 +0800 Subject: [PATCH] 更新五彩ws连接处理; --- src/mes/indexMes/index.js | 42 +++++++++++++++++++++++++++++++++++------- src/models/app.js | 16 +++++++++++++--- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/mes/indexMes/index.js b/src/mes/indexMes/index.js index 1380309..9f838fb 100644 --- a/src/mes/indexMes/index.js +++ b/src/mes/indexMes/index.js @@ -334,26 +334,54 @@ const useIndexMesEvent = props => { [currentContent, sModelsId, sModelType] ); + const { webSocket: ws } = props.app; + const { url } = ws || {}; + const wsRef = useRef(ws); + useEffect(() => { + if (url) { + wsRef.current = ws; + } + }, [url]); + useEffect(() => { const connectWs = () => { - if ( - props.app.webSocket === null || - props.app.webSocket?.readyState !== WebSocket.OPEN - ) { - console.log("================webSocket连接======================"); + if (!wsRef.current) { + console.log("================webSocket不存在,创建webSocket连接======================"); props.dispatch({ type: "app/createWebSocket", payload: { reStart: true, dispatch: props.dispatch } }); + return; } + if (wsRef.current.readyState !== WebSocket.OPEN) return; + const message = { + connectTest: 'test', + key: 'test', + flag: 'test', + msg: 'test', + sId: 'test', + showType: 'test', + sendFrom: props.app.userinfo.sId, + }; + wsRef.current.send(JSON.stringify(message)); + window.wsTimer = setTimeout(() => { + console.log("================未收到Test返回消息,webSocket重新连接======================"); + props.dispatch({ + type: "app/createWebSocket", + payload: { reStart: true, dispatch: props.dispatch } + }); + }, 3000); }; connectWs(); const timer = setInterval(() => { connectWs(); - }, 50000); + }, 20000); - return () => clearInterval(timer); + return () => { + clearInterval(timer); + clearTimeout(window.wsTimer); + } }, []); useEffect(() => { diff --git a/src/models/app.js b/src/models/app.js index a80e366..2e39d7d 100644 --- a/src/models/app.js +++ b/src/models/app.js @@ -8,7 +8,7 @@ import { Toast } from 'antd-mobile'; import * as commonFunc from '../components/Common/commonFunc';/* 通用单据方法 */ if (location.pathname === '/' || location.pathname === '/login') { - localStorage.clear(); + // localStorage.clear(); } export default { @@ -575,14 +575,14 @@ export default { if (userinfo.sUserLoginType) { url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`; } - if (reStart) { + if (reStart || true) { // const webSocket = yield select(state => state.app.webSocket); // if (webSocket === null) { // return null; // } url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true`; if (userinfo.sUserLoginType) { - url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&sLoginType=${userinfo.sUserLoginType}`; + url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`; } } const reset = (ws, config1) => { @@ -596,13 +596,23 @@ export default { ws.send(JSON.stringify(message)); }, config1.timeoutServer); }; + const oldWebSocket = yield select(state => state.app.webSocket); const ws = new WebSocket(url); ws.onopen = function (e) { console.log('连接上 webscoket 服务端了', e); start(ws, config); + if (oldWebSocket && oldWebSocket !== ws) { + try { + oldWebSocket.close(); + console.log('================已关闭旧的WebSocket连接======================'); + } catch (error) { + console.log('================关闭旧WebSocket连接时出错======================', error); + } + } }; // 全局通用的自定义onmessage的方法 ws.homeAction = (msg) => { + clearTimeout(window.wsTimer); const rtmsg = JSON.parse(msg.data); if (false && rtmsg.action === 'showImg' && location.pathname.indexOf('/indexOee') < 0) { const msgData = JSON.parse(msg.data); -- libgit2 0.22.2