'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const {
  invokeCallbackHandler: invoke
} = UniServiceJSBridge
const eventNames = ['open', 'close', 'error', 'message']
const callbacks = {}
const socketTasks = []
/**
 * SocketTask
 */
class SocketTask {
  /**
   * WebSocket实例
   */
  _webSocket
  /**
   * 构造函数
   * @param {string} url
   * @param {Array} protocols
   */
  constructor (url, protocols, callback) {
    let error
    try {
      const webSocket = this._webSocket = new WebSocket(url, protocols)
      webSocket.binaryType = 'arraybuffer'
      this._callbacks = {}
      eventNames.forEach(name => {
        this._callbacks[name] = []
        webSocket.addEventListener(name, event => {
          const { data, code, reason } = event
          const res = name === 'message'
            ? { data }
            : name === 'close'
              ? { code, reason }
              : {}
          this._callbacks[name].forEach(callback => {
            try {
              callback(res)
            } catch (e) {
              console.error(`thirdScriptError\n${e};at socketTask.on${name[0].toUpperCase() + name.substr(1)} callback function\n`, e)
            }
          })
          if (this === socketTasks[0] && callbacks[name]) {
            invoke(callbacks[name], res)
          }
          if (name === 'error' || name === 'close') {
            const index = socketTasks.indexOf(this)
            if (index >= 0) {
              socketTasks.splice(index, 1)
            }
          }
        })
      })
      const propertys = ['CLOSED', 'CLOSING', 'CONNECTING', 'OPEN', 'readyState']
      propertys.forEach((property) => {
        Object.defineProperty(this, property, {
          get () {
            return webSocket[property]
          }
        })
      })
    } catch (e) {
      error = e
    }
    callback(error, this)
  }
 
  /**
   * 发送
   * @param {any} data
   */
  send (options = {}) {
    const data = options.data
    const ws = this._webSocket
    try {
      if (ws.readyState !== ws.OPEN) {
        throw new Error('SocketTask.readyState is not OPEN')
      }
      ws.send(data)
      this._callback(options, 'sendSocketMessage:ok')
    } catch (error) {
      this._callback(options, `sendSocketMessage:fail ${error}`)
    }
  }
 
  /**
   * 关闭
   * @param {number} code
   * @param {string} reason
   */
  close (options = {}) {
    const ws = this._webSocket
    const arrgs = []
    arrgs.push(options.code || 1000)
    if (typeof options.reason === 'string') {
      arrgs.push(options.reason)
    }
    try {
      ws.close(...arrgs)
      this._callback(options, 'closeSocket:ok')
    } catch (error) {
      this._callback(options, `closeSocket:fail ${error}`)
    }
  }
 
  /**
   * 通用回调处理
   */
  _callback ({
    success,
    fail,
    complete
  }, errMsg) {
    const data = {
      errMsg
    }
    if (/:ok$/.test(errMsg)) {
      if (typeof success === 'function') {
        success(data)
      }
    } else {
      if (typeof fail === 'function') {
        fail(data)
      }
    }
    if (typeof complete === 'function') {
      complete(data)
    }
  }
}
eventNames.forEach(item => {
  const name = item[0].toUpperCase() + item.substr(1)
  SocketTask.prototype[`on${name}`] = function (callback) {
    this._callbacks[item].push(callback)
  }
})
/**
 * 创建一个 WebSocket 连接
 * @param {any} data 数据
 * @return {SocketTask}
 */
export function connectSocket ({
  url,
  protocols
}, callbackId) {
  return new SocketTask(url, protocols, (error, socketTask) => {
    if (!error) {
      socketTasks.push(socketTask)
    }
    invoke(callbackId, {
      errMsg: 'connectSocket:' + (error ? `fail ${error}` : 'ok')
    })
  })
}
/**
 * 通过 WebSocket 连接发送数据
 * @param {any} options
 * @param {string} callbackId
 */
export function sendSocketMessage (options, callbackId) {
  const socketTask = socketTasks[0]
  if (socketTask && socketTask.readyState === socketTask.OPEN) {
    socketTask.send(Object.assign({}, options, {
      complete (res) {
        invoke(callbackId, res)
      }
    }))
  } else {
    invoke(callbackId, {
      errMsg: 'sendSocketMessage:fail WebSocket is not connected '
    })
  }
}
/**
 * 关闭WebSocket连接
 * @param {any} options
 * @param {string} callbackId
 */
export function closeSocket (options, callbackId) {
  const socketTask = socketTasks[0]
  if (socketTask) {
    socketTask.close(Object.assign({}, options, {
      complete (res) {
        invoke(callbackId, res)
      }
    }))
  } else {
    invoke(callbackId, {
      errMsg: 'closeSocket:fail WebSocket is not connected'
    })
  }
}
/**
 * 监听事件
 * @param {string} method
 */
function on (method) {
  return function (callbackId) {
    callbacks[method] = callbackId
  }
}
/**
 * 监听WebSocket连接打开事件
 * @param {Function} cb
 */
export const onSocketOpen = on('open')
/**
 * 监听WebSocket错误
 * @param {Function} cb
 */
export const onSocketError = on('error')
/**
 * 监听WebSocket接受到服务器的消息事件
 * @param {Function} cb
 */
export const onSocketMessage = on('message')
/**
 * 监听WebSocket关闭
 * @param {Function} callback
 */
export const onSocketClose = on('close')