Commit 648e0332 authored by caicaicai's avatar caicaicai

修改

parent 176ac645
...@@ -135,6 +135,7 @@ import * as echarts from 'echarts'; ...@@ -135,6 +135,7 @@ import * as echarts from 'echarts';
export default { export default {
data() { data() {
return { return {
heartbeatTimer:null,
websocket: null, websocket: null,
whetherShow:false, whetherShow:false,
selectCarFleet:'', //搜索对应车队 selectCarFleet:'', //搜索对应车队
...@@ -149,10 +150,7 @@ export default { ...@@ -149,10 +150,7 @@ export default {
dispatchBtnTitle1:'人工调度', dispatchBtnTitle1:'人工调度',
} }
}, },
created(){ created(){ },
//页面刚进入时开启长连接
this.initWebSocket();
},
mounted() { }, mounted() { },
methods: { methods: {
//获取总数据 //获取总数据
...@@ -204,6 +202,13 @@ export default { ...@@ -204,6 +202,13 @@ export default {
//车辆区域分布Echarts //车辆区域分布Echarts
this.carsAreaDistributionEcharts(); this.carsAreaDistributionEcharts();
}) })
//页面刚进入时开启长连接
this.initWebSocket();
this.heartbeatTimer = setInterval(() => {
this.reconnect();
}, 10000)
}else{ }else{
this.whetherShow = false; this.whetherShow = false;
} }
...@@ -399,12 +404,13 @@ export default { ...@@ -399,12 +404,13 @@ export default {
//初始化weosocket //初始化weosocket
initWebSocket(){ initWebSocket(){
//const wsuri = "ws://114.116.108.246:81/websocket/socketServer?userId="+userId;//连接地址,可加参数 //const wsuri = "ws://114.116.108.246:81/websocket/socketServer?userId="+userId;//连接地址,可加参数
const wsuri = process.env.VUE_APP_WS_API + "/" + 7; const wsuri = process.env.VUE_APP_WS_API + "/" + 'pcBigScreen';
this.websocket = new WebSocket(wsuri); this.websocket = new WebSocket(wsuri);
this.websocket.onopen = this.websocketonopen; this.websocket.onopen = this.websocketonopen;
this.websocket.onerror = this.websocketonerror; this.websocket.onerror = this.websocketonerror;
this.websocket.onmessage = this.websocketonmessage; this.websocket.onmessage = this.websocketonmessage;
this.websocket.onclose = this.websocketclose; this.websocket.onclose = this.websocketclose;
this.websocketonmessage();
}, },
//WebSocket连接成功 //WebSocket连接成功
websocketonopen() { websocketonopen() {
...@@ -429,11 +435,29 @@ export default { ...@@ -429,11 +435,29 @@ export default {
websocketclose(e){ websocketclose(e){
console.log("connection closed (" + e.code + ")"); console.log("connection closed (" + e.code + ")");
}, },
//WebSocket重连
reconnect(){
var This=this;
if (this.websocket.readyState === 1) { // that.websock.readyState = 1 表示连接成功,可以立即发送信息
This.websocketonmessage();
} else if (this.websocket.readyState === 0) { // 表示正在连接,设置300ms后发送信息
setTimeout(function () {
This.websocketonmessage();
}, 300);
} else { // 连接未创建或者创建失败,则重新创建连接,并设置500ms后发送信息
This.initWebSocket();
setTimeout(function () {
This.websocketonmessage();
}, 500);
}
},
}, },
destroyed: function() { destroyed: function() {
//页面销毁时关闭长连接 //页面销毁时关闭长连接
this.websocketclose(); this.websocketclose();
//关闭心跳
this.heartbeatTimer = null;
}, },
} }
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment