提交 8367fc8c authored 作者: GOD_ZYX's avatar GOD_ZYX

修改socket,在服务器更新后,自动重连

上级 580cddfc
...@@ -7,9 +7,13 @@ import Base64 from 'Base64' ...@@ -7,9 +7,13 @@ import Base64 from 'Base64'
let socket = null let socket = null
let auth = '' let auth = ''
let socketHeart = null
export default { export default {
data () {
return {
socketHeart: null
}
},
mounted () { mounted () {
if (window.WebSocket) { if (window.WebSocket) {
try { try {
...@@ -40,7 +44,8 @@ export default { ...@@ -40,7 +44,8 @@ export default {
} }
} }
_socket.onopen = function (e) { _socket.onopen = function (e) {
socketHeart = that.SocketHeart() that.socketHeart && clearInterval(that.socketHeart)
that.socketHeart = that.createSocketHeart()
} }
_socket.onclose = function (e) { _socket.onclose = function (e) {
/* Socket连接关闭,则重连 */ /* Socket连接关闭,则重连 */
...@@ -75,12 +80,12 @@ export default { ...@@ -75,12 +80,12 @@ export default {
} }
}, },
/* 创建心跳,保证客户端永远跟服务器 处于连接状态,不能通信则重新创建连接 */ /* 创建心跳,保证客户端永远跟服务器 处于连接状态,不能通信则重新创建连接 */
SocketHeart () { createSocketHeart () {
return setInterval(() => { return setInterval(() => {
if (socket) { if (socket && socket.readyState !== 1) {
this.sendData('sendIsAlive', 'server is alive ?') socket = this.createSocket()
} }
}, 1000) }, 10000)
} }
} }
} }
......
...@@ -10,7 +10,7 @@ const sendMsg = (req, res) => { ...@@ -10,7 +10,7 @@ const sendMsg = (req, res) => {
if (global.nsSocket) { if (global.nsSocket) {
if (req.body.sendMsg === 'send') { if (req.body.sendMsg === 'send') {
com.WebSocket.sendData(req.body.arr && req.body.arr.split(','), req.body.action, req.body.val) com.WebSocket.sendData(req.body.arr && req.body.arr.split(','), req.body.action, req.body.val)
res.status(200).send({ msg: '已向客户端发送消息' }) res.status(200).send({ msg: '已向客户端发送消息', linkCount: _nsCoonPool.length })
} else { } else {
let _conArr = [] let _conArr = []
for (let i = 0; i < _nsCoonPool.length; i++) { for (let i = 0; i < _nsCoonPool.length; i++) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论