修改已知问题

This commit is contained in:
qq787203167
2022-05-31 16:11:44 +08:00
parent abc80ca9c6
commit 36a0ed400a
3 changed files with 246 additions and 103 deletions
+104 -89
View File
@@ -77,9 +77,24 @@
return parseInt(this.msg1Count)
}
},
watch: {
'$socketPublic.state.msg': {
//处理接收到的消息
handler: function(res) {
let that = this
let data = JSON.parse(res.data)
if (data.msgId) {
this.loadData()
}
}
}
},
created() {
this.loadData()
this.initWebSocket()
// this.initWebSocket()
if (store.getters.userInfo) {
this.$socketPublic.dispatch('webSocketInit')//初始化ws
}
},
mounted() {
// this.loadData()
@@ -90,10 +105,10 @@
beforeDestroy() {
},
destroyed: function() { // 离开页面生命周期函数
this.websocketOnclose()
clearInterval(this.timer)
},
// destroyed: function() { // 离开页面生命周期函数
// this.websocketOnclose()
// clearInterval(this.timer)
// },
methods: {
loadData() {
getAction(this.url.listCementByUser).then((res) => {
@@ -131,91 +146,91 @@
},
handleHoverChange(visible) {
this.hovered = visible
},
initWebSocket: function() {
// WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
var userId = store.getters.userInfo.id
var url = window._CONFIG['domianWebSocketURL'].replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId
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
},
websocketOnopen: function() {
// 添加心跳检测,每10秒发一次数据,防止连接断开(这跟服务器的设置有关,如果服务器没有设置每隔多长时间不发消息断开,可以不进行心跳设置)
let self = this
this.timer = setInterval(() => {
try {
self.websock.send('test')
console.log('发送消息')
} catch (err) {
console.log('断开了:' + err)
self.connection()
}
}, 5000)
},
websocketOnerror: function(e) {
console.log('WebSocket连接发生错误')
this.reconnect()
},
websocketOnmessage: function(e) {
if (e.data != 'test') {
this.loadData()
}
//系统通知
},
websocketOnclose: function(e) {
console.log(e)
this.websock.close()
},
websocketSend(text) { // 数据发送
console.log(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() {
that.initWebSocket()
that.lockReconnect = false
}, 5000)
},
heartCheckFun() {
var that = this
//心跳检测,每20s心跳一次
console.log(123)
that.heartCheck = {
timeout: 20000,
timeoutObj: null,
serverTimeoutObj: null,
reset: function() {
clearTimeout(this.timeoutObj)
//clearTimeout(this.serverTimeoutObj);
return this
},
start: function() {
var self = this
this.timeoutObj = setTimeout(function() {
//这里发送一个心跳,后端收到后,返回一个心跳消息,
//onmessage拿到返回的心跳就说明连接正常
that.websocketSend('HeartBeat')
console.info('客户端发送心跳')
//self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了
// that.websock.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
//}, self.timeout)
}, this.timeout)
}
}
}
// initWebSocket: function() {
// // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
// var userId = store.getters.userInfo.id
// var url = window._CONFIG['domianWebSocketURL'].replace('https://', 'wss://').replace('http://', 'ws://') + '/websocket/' + userId
// 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
// },
// websocketOnopen: function() {
// // 添加心跳检测,每10秒发一次数据,防止连接断开(这跟服务器的设置有关,如果服务器没有设置每隔多长时间不发消息断开,可以不进行心跳设置)
// let self = this
// this.timer = setInterval(() => {
// try {
// self.websock.send('test')
// console.log('发送消息')
// } catch (err) {
// console.log('断开了:' + err)
// self.connection()
// }
// }, 5000)
// },
// websocketOnerror: function(e) {
// console.log('WebSocket连接发生错误')
// this.reconnect()
// },
// websocketOnmessage: function(e) {
// if (e.data != 'test') {
// this.loadData()
// }
// //系统通知
// },
// websocketOnclose: function(e) {
// console.log(e)
// this.websock.close()
// },
// websocketSend(text) { // 数据发送
// console.log(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() {
// that.initWebSocket()
// that.lockReconnect = false
// }, 5000)
// },
// heartCheckFun() {
// var that = this
// //心跳检测,每20s心跳一次
// console.log(123)
// that.heartCheck = {
// timeout: 20000,
// timeoutObj: null,
// serverTimeoutObj: null,
// reset: function() {
// clearTimeout(this.timeoutObj)
// //clearTimeout(this.serverTimeoutObj);
// return this
// },
// start: function() {
// var self = this
// this.timeoutObj = setTimeout(function() {
// //这里发送一个心跳,后端收到后,返回一个心跳消息,
// //onmessage拿到返回的心跳就说明连接正常
// that.websocketSend('HeartBeat')
// console.info('客户端发送心跳')
// //self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了
// // that.websock.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
// //}, self.timeout)
// }, this.timeout)
// }
// }
// }
}
}
</script>