sean.zhou
2022-03-25 af7dab1dc5d74684410d201ad498bf71657ebc16
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
import ReconnectingWebSocket from 'reconnecting-websocket'
import { CURRENT_CONFIG as config } from '/@/api/http/config'
 
let socket = {}
 
export default {
  init (getMsgFunc) {
    const token = localStorage.getItem('x-auth-token')
    const wspath =
      config.websocketURL + '?x-auth-token=' + escape(token)
    socket = new ReconnectingWebSocket(wspath)
    socket.onopen = this.onOpen
    socket.onerror = this.onError
    socket.onmessage = getMsgFunc
    socket.onclose = this.onClose
    return socket
  },
  onOpen () {
    console.log('ws opened')
  },
  onError (err) {
    console.error(err)
  },
  onClose () {
    console.log('ws closed')
  },
  sendMsg (data) {
    this.socket.send(data)
  }
}