shikeying
2023-04-19 529f48641122af7c0aec185e4283d02e97aa0f89
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
import {isEmptyValue} from '@/utils/StringUtils'
import store from "@/store";
 
/**
 * 定义'Websocket'连接对象。
 * @author 时克英
 * @date 2023-04-17
 */
class WebConnection {
  // static uid;
  // static uri;
  constructor(_uri, _uid) {
    if(isEmptyValue(_uri)){
      throw 'uri参数为空';
    }
    if(isEmptyValue(_uid)){
      throw 'uid参数为空';
    }
    this.uri = _uri;
    this.uid = _uid;
    this.socket = null;
    this._ws_timer = setInterval(this.timedCheckConnection, 120000);  // 定时任务
  }
 
  shutdown(){
    if(this.socket != null && this.socket != undefined){
      this.socket.close();
    }
  }
 
  startConnect(){
    if('WebSocket' in window){
      this.socket = new WebSocket(this.uri);
      this.socket.onopen = this.wsOpen;
      this.socket.onmessage = this.wsMessage;
      this.socket.onerror = this.wsError;
      this.socket.onclose = this.wsClose;
    } else {
      this.socket = null;
      console.log('创建ws错误,可能当前浏览器不支持webSocket')
    }
  }
 
  timedCheckConnection(){
    let uri = store.state.user.uri;
    let uid = store.state.user.uid;
    if(!isEmptyValue(uri) && !isEmptyValue(uid)){
      // 2023-04-18,这里也无法直接使用:this.socket,因为此时为空(undefined)
      // console.log("是否连接 = " + this.socket);
      // if(this.socket == null || this.socket == undefined || !this.connected()){
      if(store.state.user.webConnection == null || !store.state.user.webConnection.connected()){
        this.uri = uri;
        this.uid = uid;
        console.log("重新连接:" + this.uri);
        store.state.user.webConnection.startConnect();
      }
    } else{
      console.error("uri或uid为空," + this.uid + ", " + this.uri);
    }
  }
  wsOpen(){
    // 2023-04-18,注意:这里回调方法中无法直接获取:this.uid,猜测是因为上下文环境已经变化(类似于匿名类换成独立类的情况)
    // 因此,经过长时间研究,决定需要放在全局store中。
    let uid = store.state.user.uid;
    console.log("--------> uid = " + uid);
    this.send(JSON.stringify({"protocol":"login", "uid":uid}));
    // console.log(this);
  }
  wsClose(){
    this.uid = store.state.uid;
    console.log("浏览器断开连接:" + this.uid);
  }
  wsError(err){
    console.log('== websocket error ==', err);
  }
  wsMessage(event){
    // console.log(event);
    if(event.data == null || event.data == "" || event.data == "null"){
      console.log("接收到服务端空数据\n");
      return;
    }
    store.state.user.webConnection.processServerRequest(eval("("+ event.data +")"));
  }
 
  processServerRequest(data){
    // console.log(data);
    if(data == null){
      return;
    }
    if(data.protocol == "heartbeat"){
      return;
    }
    if(data.protocol == "login"){
      if(data.status == 0){
        // uid = data.uid;
        //$("#showText").append("服务已连接,开始提问:" + data.uid + "\n");
        console.log("浏览器连接成功:" + data.uid);
      } else {
        console.log("登录认证失败:" + data.status + "\n");
      }
      return;
    }
 
    if(data.protocol == "data" || data.protocol == "broadcast"){
      console.log("接收到web推送:");
      console.log(data.data + "\n");
      store.commit('SET_WS_MSG', data.data);
      // if(data.data.touch == 1){
      //   touchEvent(data.data);
      // } else {
      //   voiceEvent(data.data);
      // }
    }
  }
 
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  //~ 内部私有方法
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  send(message){
    if(this.socket == null){
      return;
    }
    if (this.socket.readyState == WebSocket.OPEN) {
      this.socket.send(message);
    } else {
      console.log("connection is not start.");
    }
  }
 
  connected(){
    if(this.socket == null || this.socket == undefined){
      return false;
    }
    if(this.socket.readyState == 1){
      return true;
    }
    return false;
  }
 
}
 
export default WebConnection;