/** * 定义'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;