|
@@ -1,13 +1,10 @@
|
|
-const userId = localStorage.getItem('userId')
|
|
|
|
-const randomNum = localStorage.getItem('randomNum')
|
|
|
|
-
|
|
|
|
export default {
|
|
export default {
|
|
namespaced: true,
|
|
namespaced: true,
|
|
state: {
|
|
state: {
|
|
websock: null,
|
|
websock: null,
|
|
- url: 'ws://112.74.164.79:10088/web_socket/' + userId + '_' + randomNum,
|
|
|
|
|
|
+ url: '',
|
|
lockReconnect: false, // 是否真正建立连接
|
|
lockReconnect: false, // 是否真正建立连接
|
|
- timeout: 25 * 1000, // 25秒一次心跳
|
|
|
|
|
|
+ timeout: 30 * 1000, // 25秒一次心跳
|
|
timeoutObj: null, // 心跳心跳倒计时
|
|
timeoutObj: null, // 心跳心跳倒计时
|
|
serverTimeoutObj: null, // 心跳倒计时
|
|
serverTimeoutObj: null, // 心跳倒计时
|
|
timeoutnum: null, // 断开 重连倒计时
|
|
timeoutnum: null, // 断开 重连倒计时
|
|
@@ -20,27 +17,26 @@ export default {
|
|
},
|
|
},
|
|
mutations: {
|
|
mutations: {
|
|
WEBSOCKET_INIT (state, url) {
|
|
WEBSOCKET_INIT (state, url) {
|
|
|
|
+ if (!state || !url) return
|
|
const that = this
|
|
const that = this
|
|
state.websock = new WebSocket(url)
|
|
state.websock = new WebSocket(url)
|
|
state.url = url
|
|
state.url = url
|
|
state.websock.onopen = function () {
|
|
state.websock.onopen = function () {
|
|
- console.log('连接成功') // 发送用户JWT令牌 后端解析后自动绑定用户
|
|
|
|
- // state.websock.send('id:123')
|
|
|
|
- // state.websock.send('OpenBarScanner')
|
|
|
|
|
|
+ console.log('连接成功')
|
|
// 发送心跳包
|
|
// 发送心跳包
|
|
that.commit('websocket/start')
|
|
that.commit('websocket/start')
|
|
}
|
|
}
|
|
state.websock.onmessage = function (callBack) {
|
|
state.websock.onmessage = function (callBack) {
|
|
- // console.log(callBack.data)
|
|
|
|
// 重置心跳
|
|
// 重置心跳
|
|
- // console.log(callBack.data)
|
|
|
|
|
|
+ // console.log('callBack = ' + callBack.data)
|
|
that.commit('websocket/reset')
|
|
that.commit('websocket/reset')
|
|
if (callBack.data === 'heartCheck') {
|
|
if (callBack.data === 'heartCheck') {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- state.message = callBack.data
|
|
|
|
|
|
+ // state.message = callBack.data
|
|
|
|
+ state.message = JSON.parse(JSON.stringify(callBack.data))
|
|
}
|
|
}
|
|
- state.websock.οnerrοr = function () { // e错误
|
|
|
|
|
|
+ state.websock.onerror = function () { // e错误
|
|
// console.log(e)
|
|
// console.log(e)
|
|
that.commit('websocket/reconnect')
|
|
that.commit('websocket/reconnect')
|
|
}
|
|
}
|
|
@@ -53,8 +49,7 @@ export default {
|
|
state.websock.send(message)
|
|
state.websock.send(message)
|
|
},
|
|
},
|
|
reconnect (state) { // 重新连接
|
|
reconnect (state) { // 重新连接
|
|
- // console.log("重新连接")
|
|
|
|
- var that = this
|
|
|
|
|
|
+ let that = this
|
|
if (state.lockReconnect) {
|
|
if (state.lockReconnect) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -65,7 +60,7 @@ export default {
|
|
// 新连接
|
|
// 新连接
|
|
that.commit('websocket/WEBSOCKET_INIT', state.url)
|
|
that.commit('websocket/WEBSOCKET_INIT', state.url)
|
|
state.lockReconnect = false
|
|
state.lockReconnect = false
|
|
- }, 5000)
|
|
|
|
|
|
+ }, 15000)
|
|
},
|
|
},
|
|
reset (state) { // 重置心跳
|
|
reset (state) { // 重置心跳
|
|
// 清除时间
|
|
// 清除时间
|
|
@@ -74,9 +69,9 @@ export default {
|
|
// 心跳
|
|
// 心跳
|
|
this.commit('websocket/start')
|
|
this.commit('websocket/start')
|
|
},
|
|
},
|
|
- start (state) { // 开启心跳
|
|
|
|
- // console.log("开启心跳")
|
|
|
|
- var that = this
|
|
|
|
|
|
+ start (state) {
|
|
|
|
+ // 开启心跳
|
|
|
|
+ let that = this
|
|
state.timeoutObj && clearTimeout(state.timeoutObj)
|
|
state.timeoutObj && clearTimeout(state.timeoutObj)
|
|
state.serverTimeoutObj && clearTimeout(state.serverTimeoutObj)
|
|
state.serverTimeoutObj && clearTimeout(state.serverTimeoutObj)
|
|
state.timeoutObj = setTimeout(function () {
|
|
state.timeoutObj = setTimeout(function () {
|