initWebSocket() {
//初始化weosocket(必须)
// if(!this.createTaskId || !this.headTaskId){
// return
// }
if (this.protocolHost == "video.chci.cn") {
this.protocolHost = "ts.chci.cn";
}
const wsuri =
// `wss://${this.protocolHost}/videoUI/dailyTv/channelId/?taskId=` +
// this.headTaskId; //请根据实际项目需要进行修改
`${process.env.NODE_ENV == "development" ? "ws" : "ws"}://${
this.protocolHost
}/videoUI/comparation/` + this.headTaskId; //请根据实际项目需要进行修改
this.websock = new WebSocket(wsuri); //新建一个webstock对象
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
websocketreconnect() {
//重新连接 心跳机制
var that = this;
if (that.lockReconnect) {
return;
}
that.lockReconnect = true;
//没连接上会一直重连,设置延迟避免请求过多
that.timeoutnum && clearTimeout(that.timeoutnum);
that.timeoutnum = setTimeout(function() {
//新连接
if (that.lockReconnect) {
return;
}
console.log("aaa");
that.initWebSocket();
}, 5000);
},
reset() {
//重置心跳
var that = this;
//清除时间
clearTimeout(that.timeoutObj);
clearTimeout(that.serverTimeoutObj);
//重启心跳
that.websocketstart();
},
websocketstart() {
//开启心跳
var self = this;
self.timeoutObj && clearTimeout(self.timeoutObj);
self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj);
self.timeoutObj = setTimeout(function() {
//这里发送一个心跳,后端收到后,返回一个心跳消息,
if (self.websock.readyState == 1) {
//如果连接正常
self.websock.send("heartCheck"); //这里可以自己跟后端约定
}
}, self.timeout);
},
websocketonopen(e) {
//websocket连接后发送数据(send发送)
console.log("连接成功");
// this.websocketstart();
},
websocketonerror(e) {
//连接建立失败重连
console.log("websocketonerror", e);
//重连
//this.websocketreconnect();
},
websocketonmessage(e) {
//数据接收
var WEBSCOKET = JSON.parse(e.data);
// console.log("疑似结果", WEBSCOKET);
if (WEBSCOKET !== "") {
this.accept(WEBSCOKET);
}
//收到服务器信息,心跳重置
this.reset();
},
websocketsend(Data) {
//数据发送
this.websock.send(Data);
},
websocketclose(e) {
//关闭
let that = this;
// this.websocketreconnect();
if (that.activeStaus == 2) {
console.log("状态威2");
that.websocketreconnect();
}
// if (this.activeStaus == 0) {
// this.websock.close();
// return;
// console.log("关闭", e);
// }
// this.websocketreconnect();
},