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
143
144
145
146
/**
 * 定义'Websocket'连接对象。
 * @author 时克英
 * @date 2023-04-17
 * @date 2023-04-18 对象废弃,参考:web-connection.js
 */
const WebConnection = {
  uri: null,  // 连接地址
  _uid: null,  // 用户身份
  $webSocket: null,
  _ws_timer: null,  // 定时任务
 
  // /**
  //  * 默认构造函数
  //  * @constructor
  //  */
  // WebConnection: function (){
  //   console.log("..........定时任务");
  //   this._ws_timer = setInterval(this.timedCheckConnection, 60000);
  // },
 
  setInfo: function (uri, uid){
    this.uri = uri;
    this._uid = uid + '';
    console.log("..........定时任务: uid=" + this._uid);
    this._ws_timer = setInterval(this.timedCheckConnection, 60000);
  },
 
  startConnect: function (){
    if(this.uri != null && this.uri == '-1'){
      console.log("未启用websocket");
      return;
    }
    // if(this.uri == null){
    //   getWebsocketUri().then(response => {
    //     this.uri = response.data.uri;
    //     this.uid = response.data.uid;
    //   }).catch(()=>{
    //     this.uri = null;
    //     this.uid = null;
    //     console.log("异常");
    //   });
    // }
    if('WebSocket' in window){
      this.$webSocket = new WebSocket(this.uri);
      this.$webSocket.onopen = this.wsOpen;
      this.$webSocket.onmessage = this.wsMessage;
      this.$webSocket.onerror = this.wsError;
      this.$webSocket.onclose = this.wsClose;
    } else {
      this.$webSocket = null;
      console.log('创建ws错误,可能当前浏览器不支持webSocket')
    }
  },
 
  send: function (message){
    if(this.$webSocket == null){
      return;
    }
    if (this.$webSocket.readyState == WebSocket.OPEN) {
      this.$webSocket.send(message);
    } else {
      console.log("connection is not start.");
    }
  },
 
  connected: function (){
    if(this.$webSocket == null || this.$webSocket == undefined){
      return false;
    }
    if(this.$webSocket.readyState == 1){
      // connected
      return true;
    }
    return false;
  },
 
  wsOpen: function() {
    // this.send(JSON.stringify({"protocol":"login", "uid":this.uid}));
    let loginInfo = "{\"protocol\":\"login\", \"uid\":\"" + this._uid + "\"}";
    this.send(loginInfo);
    console.log('== websocket open ==' + this._uid);
  },
 
  wsClose: function (){
    this._uid = null;
    console.log("浏览器断开连接:" + this._uid);
  },
  wsError: function(err){
    console.log('== websocket error ==', err)
  },
 
  wsMessage:function(event) {
    if(event.data == null || event.data == "" || event.data == "null"){
//                $("#showText").append("接收到服务端空数据\n");
      console.log("接收到服务端空数据\n");
      return;
    }
    this.processServerRequest(eval("("+ event.data +")"));
  },
 
  processServerRequest: function (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"){
      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);
      // }
    }
  },
 
  timedCheckConnection: function (){
    if(this.uri != null && this.uri != "" && this.uri != undefined
          && this._uid != null && this._uid != "" && this._uid != undefined){
      if(this.$webSocket == null || this.$webSocket == undefined || !this.connected()){
        console.log("重新连接:" + this.uri);
        // webConnection = new WebConnection(_wurl, _uid, null);
        this.startConnect();
      }
    } else{
      console.error("uri或uid为空," + this._uid + ", " + this.uri);
    }
  }
}
 
export default WebConnection;