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;
|