From 068fb7c09592feca14a68675e9e9c61d9b6bd3f2 Mon Sep 17 00:00:00 2001 From: pengm <674192343@qq.com> Date: Mon, 12 Jan 2026 09:46:10 +0800 Subject: [PATCH] 1.处理websoket断网后,或是后端发布,在重新连接后,前端不在重新连接websoket --- src/mes/common/commonOperationBarComponent/index.js | 2 +- src/models/app.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/mes/common/commonOperationBarComponent/index.js b/src/mes/common/commonOperationBarComponent/index.js index a20e5c3..2cbf507 100644 --- a/src/mes/common/commonOperationBarComponent/index.js +++ b/src/mes/common/commonOperationBarComponent/index.js @@ -1138,7 +1138,7 @@ const CommonOperationBarComponent = basProps => { searchProductProps.componentStyle = { width: 230 }; } } -console.log(searchProductProps, 'searchProductProps'); + const handleBtnSearch = () => { const { sModelsId } = props; diff --git a/src/models/app.js b/src/models/app.js index e912852..9763d89 100644 --- a/src/models/app.js +++ b/src/models/app.js @@ -582,12 +582,12 @@ export default { if (userinfo.sUserLoginType) { url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`; } - if (reStart || true) { + if (true) { // const webSocket = yield select(state => state.app.webSocket); // if (webSocket === null) { // return null; // } - url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true`; + url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}`; if (userinfo.sUserLoginType) { url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`; } @@ -598,6 +598,38 @@ export default { start(ws, config1); }; const start = (ws, config1) => { + if (!ws.originalSend) { + ws.originalSend = ws.send.bind(ws); + } + ws.send = (...args) => { + const value = utils.convertStrToObj(args[0], {}); + const { + key, + flag, + sendFrom, + sId, + } = value; + if ((key && flag && !['release', 'noAction'].includes(flag) && sendFrom && sId) || flag === 'connectTest') { + // 3s内没返回消息,则重新创建ws,重新发送 + clearTimeout(window.wsTimer); + window.wsTimer = setTimeout(() => { + console.log('================超时未收到WebSocket消息,重新创建ws并发送信息======================'); + window.wsHistoryArgs = args; + ws.onmessageTmp && (window.onmessageTmp = ws.onmessageTmp); + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } }); + }, 3000); + } + ws.originalSend.apply(ws, args); + }; + + if (window.wsHistoryArgs) { + ws.send(...window.wsHistoryArgs); + window.wsHistoryArgs = undefined; + } + if (window.onmessageTmp) { + ws.onmessageTmp = window.onmessageTmp; + window.onmessageTmp = undefined; + } config1.timerServer = setTimeout(() => { const message = { sendFrom: userinfo.sId, connectTest: 'test' }; // param 存放其它参数 keyName 需要放入Redis的数据key,keyValue 需要放入Redis的数据key 的值 ws.send(JSON.stringify(message)); @@ -618,8 +650,7 @@ export default { } }; // 全局通用的自定义onmessage的方法 - ws.homeAction = (msg) => { - clearTimeout(window.wsTimer); + ws.homeAction = msg => { const rtmsg = JSON.parse(msg.data); if (false && rtmsg.action === 'showImg' && location.pathname.indexOf('/indexOee') < 0) { const msgData = JSON.parse(msg.data); @@ -688,10 +719,15 @@ export default { } else if (rtmsg.action === 'showErroMsg') { console.log('=====msgData', msgData); message.error(msgData.msg); + } else if (window.tempWsAction) { + window.tempWsAction(msg); } }; ws.onmessage = (msg) => { + clearTimeout(window.wsTimer); + window.xlyWsTimerFun && window.xlyWsTimerFun(); reset(ws, config); + ws.onmessageTmp?.(msg); ws.homeAction(msg); }; ws.onclose = (e) => { -- libgit2 0.22.2