提交 580cddfc authored 作者: GOD_ZYX's avatar GOD_ZYX

修改 socket代码

上级 520322c8
...@@ -7,6 +7,7 @@ import Base64 from 'Base64' ...@@ -7,6 +7,7 @@ import Base64 from 'Base64'
let socket = null let socket = null
let auth = '' let auth = ''
let socketHeart = null
export default { export default {
mounted () { mounted () {
...@@ -38,7 +39,9 @@ export default { ...@@ -38,7 +39,9 @@ export default {
} }
} }
} }
_socket.onopen = function (e) { } _socket.onopen = function (e) {
socketHeart = that.SocketHeart()
}
_socket.onclose = function (e) { _socket.onclose = function (e) {
/* Socket连接关闭,则重连 */ /* Socket连接关闭,则重连 */
if (e.target && e.target.readyState === 2) { if (e.target && e.target.readyState === 2) {
...@@ -70,6 +73,14 @@ export default { ...@@ -70,6 +73,14 @@ export default {
} else { } else {
console.log('not link build success, status: ' + socket.readyState) console.log('not link build success, status: ' + socket.readyState)
} }
},
/* 创建心跳,保证客户端永远跟服务器 处于连接状态,不能通信则重新创建连接 */
SocketHeart () {
return setInterval(() => {
if (socket) {
this.sendData('sendIsAlive', 'server is alive ?')
}
}, 1000)
} }
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论