绿满眶商城微信小程序-uniapp
peng
8 小时以前 a6c950ba797dffec842cf7e923fc439868645766
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
import store from '@/store/index'
import config from '@/config/config.js'
import storage from './storage';
export default class SocketService {
  /**
   * 单例
   */
  static instance = null;
  static get Instance () {
    if (!this.instance) {
      this.instance = new SocketService();
    }
    return this.instance;
  }
 
  // 和服务端连接的socket对象
  ws = null;
 
  // 存储回调函数
  callBackMapping = {};
 
  // 标识是否连接成功
  connected = false;
 
  // 记录重试的次数
  sendRetryCount = 0;
 
  // 重新连接尝试的次数
  connectRetryCount = 0;
 
  //  定义连接服务器的方法
  connect () {
    // 连接服务器
    if (!window.WebSocket) {
      return console.log("您的浏览器不支持WebSocket");
    }
    this.ws = new WebSocket(config.baseWsUrl + '/' + storage.getAccessToken());
    // 连接成功的事件
    this.ws.onopen = () => {
      console.log("连接服务端成功");
      this.connected = true;
      // 重置重新连接的次数
      this.connectRetryCount = 0;
    };
    //  1.连接服务端失败
    //   2.当连接成功之后, 服务器关闭的情况(连接失败重连)
    this.ws.onclose = () => {
      console.log("连接服务端失败");
      this.connected = false;
      this.connectRetryCount++;
      setTimeout(() => {
        this.connect();
      }, 500 * this.connectRetryCount);
    };
    // 得到服务端发送过来的数据
    this.ws.onmessage = (msg) => {
      // console.log(msg.data)
      this.registerCallBack(msg.data);
    };
  }
  // 回调函数的注册
  registerCallBack (callBack) {
    // console.log("回调函数的注册", callBack);
    this.callBackMapping = callBack;
  }
 
  // 取消某一个回调函数
  unRegisterCallBack (callBack) {
    console.log("取消某一个回调函数", callBack);
    this.callBackMapping = null;
  }
 
  // 发送数据的方法
  send (data) {
    // 判断此时此刻有没有连接成功
    if (this.connected) {
      this.sendRetryCount = 0;
      this.ws.send(data);
    } else {
      this.sendRetryCount++;
      setTimeout(() => {
        this.send(data);
      }, this.sendRetryCount * 500);
    }
  }
}