Commit 068fb7c09592feca14a68675e9e9c61d9b6bd3f2

Authored by Min
1 parent 2678cd30

1.处理websoket断网后,或是后端发布,在重新连接后,前端不在重新连接websoket

src/mes/common/commonOperationBarComponent/index.js
... ... @@ -1138,7 +1138,7 @@ const CommonOperationBarComponent = basProps => {
1138 1138 searchProductProps.componentStyle = { width: 230 };
1139 1139 }
1140 1140 }
1141   -console.log(searchProductProps, 'searchProductProps');
  1141 +
1142 1142  
1143 1143 const handleBtnSearch = () => {
1144 1144 const { sModelsId } = props;
... ...
src/models/app.js
... ... @@ -582,12 +582,12 @@ export default {
582 582 if (userinfo.sUserLoginType) {
583 583 url = `${config.ws_host}websocket/${userinfo.sId}?sLoginType=${userinfo.sUserLoginType}`;
584 584 }
585   - if (reStart || true) {
  585 + if (true) {
586 586 // const webSocket = yield select(state => state.app.webSocket);
587 587 // if (webSocket === null) {
588 588 // return null;
589 589 // }
590   - url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true`;
  590 + url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}`;
591 591 if (userinfo.sUserLoginType) {
592 592 url = `${config.ws_host}websocket/${userinfo.sId}?reStart=true&createTime=${new Date().getTime()}&sLoginType=${userinfo.sUserLoginType}`;
593 593 }
... ... @@ -598,6 +598,38 @@ export default {
598 598 start(ws, config1);
599 599 };
600 600 const start = (ws, config1) => {
  601 + if (!ws.originalSend) {
  602 + ws.originalSend = ws.send.bind(ws);
  603 + }
  604 + ws.send = (...args) => {
  605 + const value = utils.convertStrToObj(args[0], {});
  606 + const {
  607 + key,
  608 + flag,
  609 + sendFrom,
  610 + sId,
  611 + } = value;
  612 + if ((key && flag && !['release', 'noAction'].includes(flag) && sendFrom && sId) || flag === 'connectTest') {
  613 + // 3s内没返回消息,则重新创建ws,重新发送
  614 + clearTimeout(window.wsTimer);
  615 + 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 } });
  620 + }, 3000);
  621 + }
  622 + ws.originalSend.apply(ws, args);
  623 + };
  624 +
  625 + if (window.wsHistoryArgs) {
  626 + ws.send(...window.wsHistoryArgs);
  627 + window.wsHistoryArgs = undefined;
  628 + }
  629 + if (window.onmessageTmp) {
  630 + ws.onmessageTmp = window.onmessageTmp;
  631 + window.onmessageTmp = undefined;
  632 + }
601 633 config1.timerServer = setTimeout(() => {
602 634 const message = { sendFrom: userinfo.sId, connectTest: 'test' }; // param 存放其它参数 keyName 需要放入Redis的数据key,keyValue 需要放入Redis的数据key 的值
603 635 ws.send(JSON.stringify(message));
... ... @@ -618,8 +650,7 @@ export default {
618 650 }
619 651 };
620 652 // 全局通用的自定义onmessage的方法
621   - ws.homeAction = (msg) => {
622   - clearTimeout(window.wsTimer);
  653 + ws.homeAction = msg => {
623 654 const rtmsg = JSON.parse(msg.data);
624 655 if (false && rtmsg.action === 'showImg' && location.pathname.indexOf('/indexOee') < 0) {
625 656 const msgData = JSON.parse(msg.data);
... ... @@ -688,10 +719,15 @@ export default {
688 719 } else if (rtmsg.action === 'showErroMsg') {
689 720 console.log('=====msgData', msgData);
690 721 message.error(msgData.msg);
  722 + } else if (window.tempWsAction) {
  723 + window.tempWsAction(msg);
691 724 }
692 725 };
693 726 ws.onmessage = (msg) => {
  727 + clearTimeout(window.wsTimer);
  728 + window.xlyWsTimerFun && window.xlyWsTimerFun();
694 729 reset(ws, config);
  730 + ws.onmessageTmp?.(msg);
695 731 ws.homeAction(msg);
696 732 };
697 733 ws.onclose = (e) => {
... ...