data() {
return {
websock: null,
}
}
methods:{
// websockets
initWebSocket() {
let httpdemo = window.location.hostname;
const urlHeader = window.location.protocol + "//";
var base = "";
if (
httpdemo === "testmqgitfrontend.meequ.cn" ||
httpdemo === "localhost" ||
httpdemo === "192.168.1.136"
) {
base = "ws://120.25.2.34:10125"; //判断在测试环境下的域名
} else {
base = "wss://mqfrontend.mizhuanbao.cn:10125"; //判断在正式环境下的域名
}
var url = base + actId
// WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
// console.log("调用了链接websock ,获取的用户id为 :"+store.getters.userInfo.id)
// console.log(url);
this.websock = new WebSocket(url);
this.websock.onopen = this.websocketOnopen;
this.websock.onerror = this.websocketOnerror;
this.websock.onmessage = this.websocketOnmessage;
this.websock.onclose = this.websocketOnclose;
console.log("Socket", this.websock);
},
websocketOnopen() {
console.log("WebSocket连接成功");
//心跳检测重置
// this.heartCheck.reset().start();
},
websocketOnerror(e) {
console.log("WebSocket连接发生错误");
this.reconnect();
},
//调用后台数据
websocketOnmessage(e) {
console.log("监听关闭" + e);
console.log("-----接收消息-------", e.data);
this.baseData();
var data = eval("(" + e.data + ")"); //解析对象
},
websocketOnclose(e) {
console.log("connection closed (" + e.code + ")");
this.reconnect();
},
websocketSend(text) {
// 数据发送
try {
this.websock.send(text);
} catch (err) {
console.log("send failed (" + err.code + ")");
}
},
reconnect() {
var that = this;
if (that.lockReconnect) return;
that.lockReconnect = true;
//没连接上会一直重连,设置延迟避免请求过多
setTimeout(function () {
console.info("尝试重连...");
that.initWebSocket();
that.lockReconnect = false;
}, 5000);
},
}
created() {
this.initWebSocket();
},
08-20
1522
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交