Commit 34528b977e792897f12a0eee995bf6d526e3fead
1 parent
cdecaeeb
1.同步最新websoket处理逻辑到AJT
Showing
2 changed files
with
144 additions
and
9 deletions
src/mes/indexMes/index.js
| @@ -340,18 +340,35 @@ const useIndexMesEvent = props => { | @@ -340,18 +340,35 @@ const useIndexMesEvent = props => { | ||
| 340 | [currentContent, sModelsId, sModelType] | 340 | [currentContent, sModelsId, sModelType] |
| 341 | ); | 341 | ); |
| 342 | 342 | ||
| 343 | + const { webSocket: ws, userinfo } = props.app; | ||
| 344 | + const { url } = ws || {}; | ||
| 345 | + const wsRef = useRef(ws); | ||
| 346 | + useEffect(() => { | ||
| 347 | + if (url) { | ||
| 348 | + wsRef.current = ws; | ||
| 349 | + } | ||
| 350 | + }, [url]); | ||
| 351 | + | ||
| 343 | useEffect(() => { | 352 | useEffect(() => { |
| 344 | const connectWs = () => { | 353 | const connectWs = () => { |
| 345 | - if ( | ||
| 346 | - props.app.webSocket === null || | ||
| 347 | - props.app.webSocket?.readyState !== WebSocket.OPEN | ||
| 348 | - ) { | ||
| 349 | - console.log("================webSocket连接======================"); | 354 | + if (!wsRef.current || wsRef.current.readyState === WebSocket.CLOSED || wsRef.current.readyState === WebSocket.CLOSING) { |
| 355 | + console.log("================webSocket不存在或已关闭,创建webSocket连接======================"); | ||
| 350 | props.dispatch({ | 356 | props.dispatch({ |
| 351 | type: "app/createWebSocket", | 357 | type: "app/createWebSocket", |
| 352 | payload: { reStart: true, dispatch: props.dispatch } | 358 | payload: { reStart: true, dispatch: props.dispatch } |
| 353 | }); | 359 | }); |
| 360 | + return; | ||
| 354 | } | 361 | } |
| 362 | + if (wsRef.current.readyState !== WebSocket.OPEN) return; | ||
| 363 | + const message = { flag: "connectTest", sId: "test", sendFrom: userinfo.sId }; | ||
| 364 | + wsRef.current.send(JSON.stringify(message)); | ||
| 365 | + window.wsTimer = setTimeout(() => { | ||
| 366 | + console.log("================未收到Test返回消息,webSocket重新连接======================"); | ||
| 367 | + props.dispatch({ | ||
| 368 | + type: "app/createWebSocket", | ||
| 369 | + payload: { reStart: true, dispatch: props.dispatch } | ||
| 370 | + }); | ||
| 371 | + }, 3000); | ||
| 355 | }; | 372 | }; |
| 356 | 373 | ||
| 357 | connectWs(); | 374 | connectWs(); |
| @@ -359,7 +376,10 @@ const useIndexMesEvent = props => { | @@ -359,7 +376,10 @@ const useIndexMesEvent = props => { | ||
| 359 | connectWs(); | 376 | connectWs(); |
| 360 | }, 50000); | 377 | }, 50000); |
| 361 | 378 | ||
| 362 | - return () => clearInterval(timer); | 379 | + return () => { |
| 380 | + clearInterval(timer); | ||
| 381 | + clearTimeout(window.wsTimer); | ||
| 382 | + } | ||
| 363 | }, []); | 383 | }, []); |
| 364 | 384 | ||
| 365 | useEffect(() => { | 385 | useEffect(() => { |
src/models/app.js
| @@ -577,22 +577,102 @@ export default { | @@ -577,22 +577,102 @@ export default { | ||
| 577 | if (userinfo.sUserLoginType) { | 577 | if (userinfo.sUserLoginType) { |
| 578 | url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`; | 578 | url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`; |
| 579 | } | 579 | } |
| 580 | - if (reStart) { | 580 | + if (true) { |
| 581 | // const webSocket = yield select(state => state.app.webSocket); | 581 | // const webSocket = yield select(state => state.app.webSocket); |
| 582 | // if (webSocket === null) { | 582 | // if (webSocket === null) { |
| 583 | // return null; | 583 | // return null; |
| 584 | // } | 584 | // } |
| 585 | - url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true`; | 585 | + url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}`; |
| 586 | if (userinfo.sUserLoginType) { | 586 | if (userinfo.sUserLoginType) { |
| 587 | - url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&sLoginType=${userinfo.sUserLoginType}`; | 587 | + url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`; |
| 588 | } | 588 | } |
| 589 | } | 589 | } |
| 590 | + // 清理旧的WebSocket连接和定时器 | ||
| 591 | + let oldWebSocket = yield select(state => state.app.webSocket); | ||
| 592 | + if (oldWebSocket) { | ||
| 593 | + // console.log('发现旧的WebSocket连接,准备清理:', oldWebSocket); | ||
| 594 | + // console.log('旧WebSocket连接状态:', oldWebSocket.readyState); | ||
| 595 | + try { | ||
| 596 | + // 移除所有事件监听器 | ||
| 597 | + oldWebSocket.onopen = null; | ||
| 598 | + oldWebSocket.onclose = null; | ||
| 599 | + oldWebSocket.onerror = null; | ||
| 600 | + oldWebSocket.onmessage = null; | ||
| 601 | + oldWebSocket.onmessageTmp = null; | ||
| 602 | + oldWebSocket.homeAction = null; | ||
| 603 | + | ||
| 604 | + // 关闭连接 | ||
| 605 | + if (oldWebSocket.readyState === WebSocket.OPEN || oldWebSocket.readyState === WebSocket.CONNECTING) { | ||
| 606 | + oldWebSocket.close(1000, '正常关闭,准备重连'); | ||
| 607 | + // console.log('已关闭旧的WebSocket连接'); | ||
| 608 | + } | ||
| 609 | + // 断开引用,帮助垃圾回收 | ||
| 610 | + oldWebSocket = null; | ||
| 611 | + console.log('旧WebSocket连接已彻底清理'); | ||
| 612 | + } catch (error) { | ||
| 613 | + console.log('关闭旧WebSocket连接时出错:', error); | ||
| 614 | + } | ||
| 615 | + } else { | ||
| 616 | + console.log('没有发现旧的WebSocket连接'); | ||
| 617 | + } | ||
| 618 | + | ||
| 619 | + // 清除所有定时器 | ||
| 620 | + clearTimeout(window.wsTimer); | ||
| 621 | + clearTimeout(config.timerServer); | ||
| 622 | + clearTimeout(config.serverTimer); | ||
| 623 | + window.wsTimer = null; | ||
| 624 | + config.timerServer = null; | ||
| 625 | + config.serverTimer = null; | ||
| 626 | + | ||
| 627 | + // 清理全局变量 | ||
| 628 | + window.wsHistoryArgs = undefined; | ||
| 629 | + window.onmessageTmp = undefined; | ||
| 590 | const reset = (ws, config1) => { | 630 | const reset = (ws, config1) => { |
| 591 | clearTimeout(config1.timerServer); | 631 | clearTimeout(config1.timerServer); |
| 592 | clearTimeout(config1.serverTimer); | 632 | clearTimeout(config1.serverTimer); |
| 593 | start(ws, config1); | 633 | start(ws, config1); |
| 594 | }; | 634 | }; |
| 595 | const start = (ws, config1) => { | 635 | const start = (ws, config1) => { |
| 636 | + if (!ws.originalSend) { | ||
| 637 | + ws.originalSend = ws.send.bind(ws); | ||
| 638 | + } | ||
| 639 | + ws.send = (...args) => { | ||
| 640 | + const value = utils.convertStrToObj(args[0], {}); | ||
| 641 | + const { | ||
| 642 | + key, | ||
| 643 | + flag, | ||
| 644 | + sendFrom, | ||
| 645 | + sId, | ||
| 646 | + } = value; | ||
| 647 | + if ((key && flag && !['release', 'noAction'].includes(flag) && sendFrom && sId) || flag === 'connectTest') { | ||
| 648 | + // 3s内没返回消息,则重新创建ws,重新发送 | ||
| 649 | + clearTimeout(window.wsTimer); | ||
| 650 | + window.wsTimer = setTimeout(() => { | ||
| 651 | + // 检查当前WebSocket连接状态 | ||
| 652 | + if (ws && ws.readyState === WebSocket.OPEN) { | ||
| 653 | + console.log('================WebSocket连接正常但超时未收到消息,重新发送信息======================'); | ||
| 654 | + // 先尝试重新发送消息,而不是直接创建新连接 | ||
| 655 | + ws.originalSend.apply(ws, args); | ||
| 656 | + } else { | ||
| 657 | + console.log('================超时未收到WebSocket消息,重新创建ws并发送信息======================'); | ||
| 658 | + window.wsHistoryArgs = args; | ||
| 659 | + ws.onmessageTmp && (window.onmessageTmp = ws.onmessageTmp); | ||
| 660 | + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } }); | ||
| 661 | + } | ||
| 662 | + }, 3000); | ||
| 663 | + } | ||
| 664 | + ws.originalSend.apply(ws, args); | ||
| 665 | + }; | ||
| 666 | + | ||
| 667 | + if (window.wsHistoryArgs) { | ||
| 668 | + ws.send(...window.wsHistoryArgs); | ||
| 669 | + window.wsHistoryArgs = undefined; | ||
| 670 | + } | ||
| 671 | + if (window.onmessageTmp) { | ||
| 672 | + ws.onmessageTmp = window.onmessageTmp; | ||
| 673 | + window.onmessageTmp = undefined; | ||
| 674 | + } | ||
| 675 | + clearTimeout(config1.timerServer); | ||
| 596 | config1.timerServer = setTimeout(() => { | 676 | config1.timerServer = setTimeout(() => { |
| 597 | const message = { sendFrom: userinfo.sId, connectTest: 'test' }; // param 存放其它参数 keyName 需要放入Redis的数据key,keyValue 需要放入Redis的数据key 的值 | 677 | const message = { sendFrom: userinfo.sId, connectTest: 'test' }; // param 存放其它参数 keyName 需要放入Redis的数据key,keyValue 需要放入Redis的数据key 的值 |
| 598 | ws.send(JSON.stringify(message)); | 678 | ws.send(JSON.stringify(message)); |
| @@ -665,19 +745,54 @@ export default { | @@ -665,19 +745,54 @@ export default { | ||
| 665 | const msgData = JSON.parse(msg.data); | 745 | const msgData = JSON.parse(msg.data); |
| 666 | dispatch({ type: 'app/changeMachineData', payload: { ...msgData.msg || {} } }); | 746 | dispatch({ type: 'app/changeMachineData', payload: { ...msgData.msg || {} } }); |
| 667 | } | 747 | } |
| 748 | + } else if (window.tempWsAction) { | ||
| 749 | + window.tempWsAction(msg); | ||
| 668 | } | 750 | } |
| 669 | }; | 751 | }; |
| 670 | ws.onmessage = (msg) => { | 752 | ws.onmessage = (msg) => { |
| 753 | + clearTimeout(window.wsTimer); | ||
| 754 | + window.xlyWsTimerFun && window.xlyWsTimerFun(); | ||
| 671 | reset(ws, config); | 755 | reset(ws, config); |
| 756 | + ws.onmessageTmp?.(msg); | ||
| 672 | ws.homeAction(msg); | 757 | ws.homeAction(msg); |
| 673 | }; | 758 | }; |
| 674 | ws.onclose = (e) => { | 759 | ws.onclose = (e) => { |
| 675 | /* 当客户端收到服务器端发送关闭请求,触发onclose事件 */ | 760 | /* 当客户端收到服务器端发送关闭请求,触发onclose事件 */ |
| 676 | console.log('webscoket关闭!!', e); | 761 | console.log('webscoket关闭!!', e); |
| 762 | + // 清理定时器 | ||
| 763 | + clearTimeout(window.wsTimer); | ||
| 764 | + clearTimeout(config.timerServer); | ||
| 765 | + clearTimeout(config.serverTimer); | ||
| 766 | + window.wsTimer = null; | ||
| 767 | + config.timerServer = null; | ||
| 768 | + config.serverTimer = null; | ||
| 769 | + console.log('3', e); | ||
| 770 | + | ||
| 771 | + // 自动重连 | ||
| 772 | + if (e.code !== 1000) { // 非正常关闭 | ||
| 773 | + console.log('WebSocket非正常关闭,将在5秒后尝试重连'); | ||
| 774 | + setTimeout(() => { | ||
| 775 | + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } }); | ||
| 776 | + }, 5000); | ||
| 777 | + } | ||
| 677 | }; | 778 | }; |
| 678 | ws.onerror = (e) => { | 779 | ws.onerror = (e) => { |
| 679 | /* 如果出现连接、处理、接收、发送数据异常 触发onerror */ | 780 | /* 如果出现连接、处理、接收、发送数据异常 触发onerror */ |
| 680 | console.log('webscoket异常!!', e); | 781 | console.log('webscoket异常!!', e); |
| 782 | + // 清理定时器 | ||
| 783 | + clearTimeout(window.wsTimer); | ||
| 784 | + clearTimeout(config.timerServer); | ||
| 785 | + clearTimeout(config.serverTimer); | ||
| 786 | + window.wsTimer = null; | ||
| 787 | + config.timerServer = null; | ||
| 788 | + config.serverTimer = null; | ||
| 789 | + console.log('4', e); | ||
| 790 | + | ||
| 791 | + // 自动重连 | ||
| 792 | + console.log('WebSocket出现异常,将在5秒后尝试重连'); | ||
| 793 | + setTimeout(() => { | ||
| 794 | + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } }); | ||
| 795 | + }, 5000); | ||
| 681 | }; | 796 | }; |
| 682 | yield put({ type: 'saveWebSocket', payload: { webSocket: ws } }); | 797 | yield put({ type: 'saveWebSocket', payload: { webSocket: ws } }); |
| 683 | }, | 798 | }, |