Commit e77ed6588324c6d5c0943f335aecbd53e1d3fb15
1 parent
45124c41
更新五彩ws连接处理;
Showing
2 changed files
with
48 additions
and
10 deletions
src/mes/indexMes/index.js
| @@ -334,26 +334,54 @@ const useIndexMesEvent = props => { | @@ -334,26 +334,54 @@ const useIndexMesEvent = props => { | ||
| 334 | [currentContent, sModelsId, sModelType] | 334 | [currentContent, sModelsId, sModelType] |
| 335 | ); | 335 | ); |
| 336 | 336 | ||
| 337 | + const { webSocket: ws } = props.app; | ||
| 338 | + const { url } = ws || {}; | ||
| 339 | + const wsRef = useRef(ws); | ||
| 340 | + useEffect(() => { | ||
| 341 | + if (url) { | ||
| 342 | + wsRef.current = ws; | ||
| 343 | + } | ||
| 344 | + }, [url]); | ||
| 345 | + | ||
| 337 | useEffect(() => { | 346 | useEffect(() => { |
| 338 | const connectWs = () => { | 347 | const connectWs = () => { |
| 339 | - if ( | ||
| 340 | - props.app.webSocket === null || | ||
| 341 | - props.app.webSocket?.readyState !== WebSocket.OPEN | ||
| 342 | - ) { | ||
| 343 | - console.log("================webSocket连接======================"); | 348 | + if (!wsRef.current) { |
| 349 | + console.log("================webSocket不存在,创建webSocket连接======================"); | ||
| 344 | props.dispatch({ | 350 | props.dispatch({ |
| 345 | type: "app/createWebSocket", | 351 | type: "app/createWebSocket", |
| 346 | payload: { reStart: true, dispatch: props.dispatch } | 352 | payload: { reStart: true, dispatch: props.dispatch } |
| 347 | }); | 353 | }); |
| 354 | + return; | ||
| 348 | } | 355 | } |
| 356 | + if (wsRef.current.readyState !== WebSocket.OPEN) return; | ||
| 357 | + const message = { | ||
| 358 | + connectTest: 'test', | ||
| 359 | + key: 'test', | ||
| 360 | + flag: 'test', | ||
| 361 | + msg: 'test', | ||
| 362 | + sId: 'test', | ||
| 363 | + showType: 'test', | ||
| 364 | + sendFrom: props.app.userinfo.sId, | ||
| 365 | + }; | ||
| 366 | + wsRef.current.send(JSON.stringify(message)); | ||
| 367 | + window.wsTimer = setTimeout(() => { | ||
| 368 | + console.log("================未收到Test返回消息,webSocket重新连接======================"); | ||
| 369 | + props.dispatch({ | ||
| 370 | + type: "app/createWebSocket", | ||
| 371 | + payload: { reStart: true, dispatch: props.dispatch } | ||
| 372 | + }); | ||
| 373 | + }, 3000); | ||
| 349 | }; | 374 | }; |
| 350 | 375 | ||
| 351 | connectWs(); | 376 | connectWs(); |
| 352 | const timer = setInterval(() => { | 377 | const timer = setInterval(() => { |
| 353 | connectWs(); | 378 | connectWs(); |
| 354 | - }, 50000); | 379 | + }, 20000); |
| 355 | 380 | ||
| 356 | - return () => clearInterval(timer); | 381 | + return () => { |
| 382 | + clearInterval(timer); | ||
| 383 | + clearTimeout(window.wsTimer); | ||
| 384 | + } | ||
| 357 | }, []); | 385 | }, []); |
| 358 | 386 | ||
| 359 | useEffect(() => { | 387 | useEffect(() => { |
src/models/app.js
| @@ -8,7 +8,7 @@ import { Toast } from 'antd-mobile'; | @@ -8,7 +8,7 @@ import { Toast } from 'antd-mobile'; | ||
| 8 | import * as commonFunc from '../components/Common/commonFunc';/* 通用单据方法 */ | 8 | import * as commonFunc from '../components/Common/commonFunc';/* 通用单据方法 */ |
| 9 | 9 | ||
| 10 | if (location.pathname === '/' || location.pathname === '/login') { | 10 | if (location.pathname === '/' || location.pathname === '/login') { |
| 11 | - localStorage.clear(); | 11 | + // localStorage.clear(); |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | export default { | 14 | export default { |
| @@ -575,14 +575,14 @@ export default { | @@ -575,14 +575,14 @@ export default { | ||
| 575 | if (userinfo.sUserLoginType) { | 575 | if (userinfo.sUserLoginType) { |
| 576 | url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`; | 576 | url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`; |
| 577 | } | 577 | } |
| 578 | - if (reStart) { | 578 | + if (reStart || true) { |
| 579 | // const webSocket = yield select(state => state.app.webSocket); | 579 | // const webSocket = yield select(state => state.app.webSocket); |
| 580 | // if (webSocket === null) { | 580 | // if (webSocket === null) { |
| 581 | // return null; | 581 | // return null; |
| 582 | // } | 582 | // } |
| 583 | url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true`; | 583 | url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true`; |
| 584 | if (userinfo.sUserLoginType) { | 584 | if (userinfo.sUserLoginType) { |
| 585 | - url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&sLoginType=${userinfo.sUserLoginType}`; | 585 | + url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`; |
| 586 | } | 586 | } |
| 587 | } | 587 | } |
| 588 | const reset = (ws, config1) => { | 588 | const reset = (ws, config1) => { |
| @@ -596,13 +596,23 @@ export default { | @@ -596,13 +596,23 @@ export default { | ||
| 596 | ws.send(JSON.stringify(message)); | 596 | ws.send(JSON.stringify(message)); |
| 597 | }, config1.timeoutServer); | 597 | }, config1.timeoutServer); |
| 598 | }; | 598 | }; |
| 599 | + const oldWebSocket = yield select(state => state.app.webSocket); | ||
| 599 | const ws = new WebSocket(url); | 600 | const ws = new WebSocket(url); |
| 600 | ws.onopen = function (e) { | 601 | ws.onopen = function (e) { |
| 601 | console.log('连接上 webscoket 服务端了', e); | 602 | console.log('连接上 webscoket 服务端了', e); |
| 602 | start(ws, config); | 603 | start(ws, config); |
| 604 | + if (oldWebSocket && oldWebSocket !== ws) { | ||
| 605 | + try { | ||
| 606 | + oldWebSocket.close(); | ||
| 607 | + console.log('================已关闭旧的WebSocket连接======================'); | ||
| 608 | + } catch (error) { | ||
| 609 | + console.log('================关闭旧WebSocket连接时出错======================', error); | ||
| 610 | + } | ||
| 611 | + } | ||
| 603 | }; | 612 | }; |
| 604 | // 全局通用的自定义onmessage的方法 | 613 | // 全局通用的自定义onmessage的方法 |
| 605 | ws.homeAction = (msg) => { | 614 | ws.homeAction = (msg) => { |
| 615 | + clearTimeout(window.wsTimer); | ||
| 606 | const rtmsg = JSON.parse(msg.data); | 616 | const rtmsg = JSON.parse(msg.data); |
| 607 | if (false && rtmsg.action === 'showImg' && location.pathname.indexOf('/indexOee') < 0) { | 617 | if (false && rtmsg.action === 'showImg' && location.pathname.indexOf('/indexOee') < 0) { |
| 608 | const msgData = JSON.parse(msg.data); | 618 | const msgData = JSON.parse(msg.data); |