Commit 7f174ff87edae39a7f0b6f480de68738f01b46a2

Authored by Min
1 parent e58711ae

1.修复websoket内存泄漏问题

src/components/Common/CommonTable/index.js
@@ -4779,7 +4779,7 @@ class CommonTableRc extends React.Component { @@ -4779,7 +4779,7 @@ class CommonTableRc extends React.Component {
4779 const processIcon = true; /* 工序操作 */ 4779 const processIcon = true; /* 工序操作 */
4780 /* 获取choose的props */ 4780 /* 获取choose的props */
4781 const chooseProcessProps = { ...this.getChooseProcessPropsTableRow(), disabled: false }; 4781 const chooseProcessProps = { ...this.getChooseProcessPropsTableRow(), disabled: false };
4782 - console.log("🚀 ~ CommonTableRc ~ processIcon:", processIcon) 4782 +
4783 /** 放置选择材料图标 */ 4783 /** 放置选择材料图标 */
4784 if (materialIcon) { 4784 if (materialIcon) {
4785 const setMaterial = commonFunc.showMessage( 4785 const setMaterial = commonFunc.showMessage(
@@ -4816,7 +4816,7 @@ class CommonTableRc extends React.Component { @@ -4816,7 +4816,7 @@ class CommonTableRc extends React.Component {
4816 tableData: props.data 4816 tableData: props.data
4817 }) 4817 })
4818 : props.tableBtnsConfig || []; 4818 : props.tableBtnsConfig || [];
4819 - 4819 +
4820 return ( 4820 return (
4821 <div 4821 <div
4822 className="operate-bar" 4822 className="operate-bar"
@@ -8104,7 +8104,7 @@ class CommonTableRc extends React.Component { @@ -8104,7 +8104,7 @@ class CommonTableRc extends React.Component {
8104 handleChooseProcessRow = () => { 8104 handleChooseProcessRow = () => {
8105 if ( 8105 if (
8106 commonUtils.isNotEmptyObject(this.props.tableProps.chooseProcessConfigRow) 8106 commonUtils.isNotEmptyObject(this.props.tableProps.chooseProcessConfigRow)
8107 - ) { 8107 + ) {
8108 const iIndex = this.props.tableProps.chooseProcessConfigRow; 8108 const iIndex = this.props.tableProps.chooseProcessConfigRow;
8109 console.log("🚀 ~ CommonTableRc ~ this.props.tableProps:", this.props) 8109 console.log("🚀 ~ CommonTableRc ~ this.props.tableProps:", this.props)
8110 this.props.onBtnChoose("process", "BtnChooseProcess", iIndex); 8110 this.props.onBtnChoose("process", "BtnChooseProcess", iIndex);
src/models/app.js
@@ -592,6 +592,46 @@ export default { @@ -592,6 +592,46 @@ export default {
592 url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`; 592 url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`;
593 } 593 }
594 } 594 }
  595 + // 清理旧的WebSocket连接和定时器
  596 + let oldWebSocket = yield select(state => state.app.webSocket);
  597 + if (oldWebSocket) {
  598 + console.log('发现旧的WebSocket连接,准备清理:', oldWebSocket);
  599 + console.log('旧WebSocket连接状态:', oldWebSocket.readyState);
  600 + try {
  601 + // 移除所有事件监听器
  602 + oldWebSocket.onopen = null;
  603 + oldWebSocket.onclose = null;
  604 + oldWebSocket.onerror = null;
  605 + oldWebSocket.onmessage = null;
  606 + oldWebSocket.onmessageTmp = null;
  607 + oldWebSocket.homeAction = null;
  608 +
  609 + // 关闭连接
  610 + if (oldWebSocket.readyState === WebSocket.OPEN || oldWebSocket.readyState === WebSocket.CONNECTING) {
  611 + oldWebSocket.close(1000, '正常关闭,准备重连');
  612 + console.log('已关闭旧的WebSocket连接');
  613 + }
  614 + // 断开引用,帮助垃圾回收
  615 + oldWebSocket = null;
  616 + console.log('旧WebSocket连接已彻底清理');
  617 + } catch (error) {
  618 + console.log('关闭旧WebSocket连接时出错:', error);
  619 + }
  620 + } else {
  621 + console.log('没有发现旧的WebSocket连接');
  622 + }
  623 +
  624 + // 清除所有定时器
  625 + clearTimeout(window.wsTimer);
  626 + clearTimeout(config.timerServer);
  627 + clearTimeout(config.serverTimer);
  628 + window.wsTimer = null;
  629 + config.timerServer = null;
  630 + config.serverTimer = null;
  631 +
  632 + // 清理全局变量
  633 + window.wsHistoryArgs = undefined;
  634 + window.onmessageTmp = undefined;
595 const reset = (ws, config1) => { 635 const reset = (ws, config1) => {
596 clearTimeout(config1.timerServer); 636 clearTimeout(config1.timerServer);
597 clearTimeout(config1.serverTimer); 637 clearTimeout(config1.serverTimer);
@@ -613,10 +653,17 @@ export default { @@ -613,10 +653,17 @@ export default {
613 // 3s内没返回消息,则重新创建ws,重新发送 653 // 3s内没返回消息,则重新创建ws,重新发送
614 clearTimeout(window.wsTimer); 654 clearTimeout(window.wsTimer);
615 window.wsTimer = setTimeout(() => { 655 window.wsTimer = setTimeout(() => {
616 - console.log('================超时未收到WebSocket消息,重新创建ws并发送信息======================');  
617 - window.wsHistoryArgs = args;  
618 - ws.onmessageTmp && (window.onmessageTmp = ws.onmessageTmp);  
619 - dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } }); 656 + // 检查当前WebSocket连接状态
  657 + if (ws && ws.readyState === WebSocket.OPEN) {
  658 + console.log('================WebSocket连接正常但超时未收到消息,重新发送信息======================');
  659 + // 先尝试重新发送消息,而不是直接创建新连接
  660 + ws.originalSend.apply(ws, args);
  661 + } else {
  662 + console.log('================超时未收到WebSocket消息,重新创建ws并发送信息======================');
  663 + window.wsHistoryArgs = args;
  664 + ws.onmessageTmp && (window.onmessageTmp = ws.onmessageTmp);
  665 + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } });
  666 + }
620 }, 3000); 667 }, 3000);
621 } 668 }
622 ws.originalSend.apply(ws, args); 669 ws.originalSend.apply(ws, args);
@@ -630,24 +677,16 @@ export default { @@ -630,24 +677,16 @@ export default {
630 ws.onmessageTmp = window.onmessageTmp; 677 ws.onmessageTmp = window.onmessageTmp;
631 window.onmessageTmp = undefined; 678 window.onmessageTmp = undefined;
632 } 679 }
  680 + clearTimeout(config1.timerServer);
633 config1.timerServer = setTimeout(() => { 681 config1.timerServer = setTimeout(() => {
634 const message = { sendFrom: userinfo.sId, connectTest: 'test' }; // param 存放其它参数 keyName 需要放入Redis的数据key,keyValue 需要放入Redis的数据key 的值 682 const message = { sendFrom: userinfo.sId, connectTest: 'test' }; // param 存放其它参数 keyName 需要放入Redis的数据key,keyValue 需要放入Redis的数据key 的值
635 ws.send(JSON.stringify(message)); 683 ws.send(JSON.stringify(message));
636 }, config1.timeoutServer); 684 }, config1.timeoutServer);
637 }; 685 };
638 - const oldWebSocket = yield select(state => state.app.webSocket);  
639 const ws = new WebSocket(url); 686 const ws = new WebSocket(url);
640 ws.onopen = function (e) { 687 ws.onopen = function (e) {
641 console.log('连接上 webscoket 服务端了', e); 688 console.log('连接上 webscoket 服务端了', e);
642 start(ws, config); 689 start(ws, config);
643 - if (oldWebSocket && oldWebSocket !== ws) {  
644 - try {  
645 - oldWebSocket.close();  
646 - console.log('================已关闭旧的WebSocket连接======================');  
647 - } catch (error) {  
648 - console.log('================关闭旧WebSocket连接时出错======================', error);  
649 - }  
650 - }  
651 }; 690 };
652 // 全局通用的自定义onmessage的方法 691 // 全局通用的自定义onmessage的方法
653 ws.homeAction = msg => { 692 ws.homeAction = msg => {
@@ -733,10 +772,40 @@ export default { @@ -733,10 +772,40 @@ export default {
733 ws.onclose = (e) => { 772 ws.onclose = (e) => {
734 /* 当客户端收到服务器端发送关闭请求,触发onclose事件 */ 773 /* 当客户端收到服务器端发送关闭请求,触发onclose事件 */
735 console.log('webscoket关闭!!', e); 774 console.log('webscoket关闭!!', e);
  775 + // 清理定时器
  776 + clearTimeout(window.wsTimer);
  777 + clearTimeout(config.timerServer);
  778 + clearTimeout(config.serverTimer);
  779 + window.wsTimer = null;
  780 + config.timerServer = null;
  781 + config.serverTimer = null;
  782 + console.log('3', e);
  783 +
  784 + // 自动重连
  785 + if (e.code !== 1000) { // 非正常关闭
  786 + console.log('WebSocket非正常关闭,将在5秒后尝试重连');
  787 + setTimeout(() => {
  788 + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } });
  789 + }, 5000);
  790 + }
736 }; 791 };
737 ws.onerror = (e) => { 792 ws.onerror = (e) => {
738 /* 如果出现连接、处理、接收、发送数据异常 触发onerror */ 793 /* 如果出现连接、处理、接收、发送数据异常 触发onerror */
739 console.log('webscoket异常!!', e); 794 console.log('webscoket异常!!', e);
  795 + // 清理定时器
  796 + clearTimeout(window.wsTimer);
  797 + clearTimeout(config.timerServer);
  798 + clearTimeout(config.serverTimer);
  799 + window.wsTimer = null;
  800 + config.timerServer = null;
  801 + config.serverTimer = null;
  802 + console.log('4', e);
  803 +
  804 + // 自动重连
  805 + console.log('WebSocket出现异常,将在5秒后尝试重连');
  806 + setTimeout(() => {
  807 + dispatch({ type: 'app/createWebSocket', payload: { reStart: true, dispatch } });
  808 + }, 5000);
740 }; 809 };
741 yield put({ type: 'saveWebSocket', payload: { webSocket: ws } }); 810 yield put({ type: 'saveWebSocket', payload: { webSocket: ws } });
742 }, 811 },