研发图纸文件管理系统-前端项目
he wei
2025-03-13 ec8d9f802eac6841165425b228ef56474636fa9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import getWsUrl from './getWsUrl';
 
/**
 * @param  {...any} urls 地址可以传多个 用来初始化多个不同的websocket实例
 * 对应的websocket对象及事件都带上对应的系号 this.SOCKET1  onWSMessage1 等
 * @returns minxins对象
 */
export default function (...urls) {
  // 重连时间间隔 默认10秒
  const reConnectDelay = 10 * 1000;
  let data = {},
    computed = {},
    ourl = {},
    methods = {},
    len = urls.length;
  const fn = () => { };
  for (let i = 0; i < len; i++) {
    let idx = i + 1;
    data['Stimer' + idx] = null;
    data['SOCKET' + idx] = null;
    ourl[idx] = getWsUrl(urls[i]);
    computed['isWSOpen' + idx] = {
      cache: false,
      get () {
        return this['SOCKET' + idx] && 1 == this['SOCKET' + idx].readyState;
      }
    };
    methods['onWSMessage' + idx] = fn;
    methods['onWSOpen' + idx] = fn;
    methods['onWSError' + idx] = ((idx) => {
      return function (Event) {
        console.log('链接失败', ourl[idx]);
        this['WSClose' + idx](Event);
      }
    })(idx);
    methods['reConnect' + idx] = function () {
      this['Stimer' + idx] = setTimeout(() => {
        this['Stimer' + idx] = null;
        this.WSInit();
      }, this.reConnectDelay);
    };
    methods['WSClose' + idx] = function (Event) {
      let socket = this['SOCKET' + idx];
      const wsUri = ourl[idx];
      if (socket) {
        switch (socket.readyState) {
          case 0:
            socket.onopen = () => {
              socket.close();
              console.log('链接关闭', wsUri, 'close事件对象:', Event);
              this['SOCKET' + idx] = null;
              // 没有event对象 则为手动关闭
              if (Event) {
                this['reConnect' + idx]();
              }
            }
            break;
          case 1:
            socket.close();
            console.log('链接关闭', wsUri, 'close事件对象:', Event);
            this['SOCKET' + idx] = null;
            // 没有event对象 则为手动关闭
            if (Event) {
              this['reConnect' + idx]();
            }
            break;
          case 2:
            socket.onclose = () => {
              console.log('链接关闭', wsUri, 'close事件对象:', Event);
              this['SOCKET' + idx] = null;
              // 没有event对象 则为手动关闭
              if (Event) {
                this['reConnect' + idx]();
              }
            }
            break;
          case 3:
            console.log('链接关闭', wsUri, 'close事件对象:', Event);
            this['SOCKET' + idx] = null;
            // 没有event对象 则为手动关闭
            if (Event) {
              this['reConnect' + idx]();
            }
            break;
        }
      }
    }
  }
  return {
    data() {
      return {
        ...data,
        reConnectDelay
      }
    },
    computed: {
      ...computed
    },
    methods: {
      ...methods,
      // 打开链接
      openSocket() {
        // 初始化WebSocket 如果已经初始化 则什么都不做
        let flag = true;
        for (let i = 0; i < len; i++) {
          let idx = i + 1;
          // 如果有任何一个socket为null 则重新初始化
          if (!this['SOCKET' + idx]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          return false;
        }
        this.WSClose();
        this.WSInit();
      },
      // 初始化
      WSInit() {
        // 未被初始化初始化
        for (let i = 0; i < len; i++) {
          let idx = i + 1;
          if (!this['isWSOpen' + idx]) {
            console.log('=====WSinit, url:', ourl[idx]);
            let SOCKET = new WebSocket(ourl[idx]);
            this['SOCKET' + idx] = SOCKET;
            this['SOCKET' + idx].onmessage = this['onWSMessage' + idx];
            this['SOCKET' + idx].onopen = this['onWSOpen' + idx];
            this['SOCKET' + idx].onerror = this['onWSError' + idx];
            this['SOCKET' + idx].onclose = this['WSClose' + idx];
          }
        }
      },
      WSClose() {
        for (let i = 0; i < len; i++) {
          let idx = i + 1;
          this['WSClose' + idx]();
        }
      },
    },
    mounted() {
      this.openSocket();
    },
    beforeDestroy() {
      this.WSClose();
    }
  }
}