|
@@ -1,7 +1,7 @@
|
|
export default {
|
|
export default {
|
|
namespaced: true,
|
|
namespaced: true,
|
|
state: {
|
|
state: {
|
|
- websock: null,
|
|
|
|
|
|
+ websock: {},
|
|
url: '',
|
|
url: '',
|
|
lockReconnect: false, // 是否真正建立连接
|
|
lockReconnect: false, // 是否真正建立连接
|
|
timeout: 30 * 1000, // 30秒一次心跳
|
|
timeout: 30 * 1000, // 30秒一次心跳
|
|
@@ -18,7 +18,7 @@ export default {
|
|
mutations: {
|
|
mutations: {
|
|
WEBSOCKET_INIT (state, url) {
|
|
WEBSOCKET_INIT (state, url) {
|
|
if (!state || !url) return
|
|
if (!state || !url) return
|
|
- const that = this
|
|
|
|
|
|
+ let that = this
|
|
state.websock = new WebSocket(url)
|
|
state.websock = new WebSocket(url)
|
|
state.url = url
|
|
state.url = url
|
|
state.websock.onopen = () => {
|
|
state.websock.onopen = () => {
|
|
@@ -34,15 +34,13 @@ export default {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
state.message = callBack.data
|
|
state.message = callBack.data
|
|
- // state.message = Object.assign([], callBack.data)
|
|
|
|
- // this.$store.state.websocket.message = JSON.parse(JSON.stringify(callBack.data))
|
|
|
|
}
|
|
}
|
|
state.websock.onerror = () => { // e错误
|
|
state.websock.onerror = () => { // e错误
|
|
- // console.log(e)
|
|
|
|
|
|
+ console.log('onerror')
|
|
that.commit('websocket/reconnect')
|
|
that.commit('websocket/reconnect')
|
|
}
|
|
}
|
|
state.websock.onclose = () => { // e关闭
|
|
state.websock.onclose = () => { // e关闭
|
|
- // console.log(e)
|
|
|
|
|
|
+ console.log('onclose')
|
|
that.commit('websocket/reconnect')
|
|
that.commit('websocket/reconnect')
|
|
}
|
|
}
|
|
},
|
|
},
|
|
@@ -57,7 +55,7 @@ export default {
|
|
state.lockReconnect = true
|
|
state.lockReconnect = true
|
|
// 没连接上会一直重连,设置延迟避免请求过多
|
|
// 没连接上会一直重连,设置延迟避免请求过多
|
|
state.timeoutnum && clearTimeout(state.timeoutnum)
|
|
state.timeoutnum && clearTimeout(state.timeoutnum)
|
|
- state.timeoutnum = setTimeout(function () {
|
|
|
|
|
|
+ state.timeoutnum = setTimeout(() => {
|
|
// 新连接
|
|
// 新连接
|
|
that.commit('websocket/WEBSOCKET_INIT', state.url)
|
|
that.commit('websocket/WEBSOCKET_INIT', state.url)
|
|
state.lockReconnect = false
|
|
state.lockReconnect = false
|
|
@@ -75,7 +73,7 @@ export default {
|
|
let that = this
|
|
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(() => {
|
|
// 这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
// 这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
// console.log(state.websock)
|
|
// console.log(state.websock)
|
|
if (state.websock.readyState === 1) { // 如果连接正常
|
|
if (state.websock.readyState === 1) { // 如果连接正常
|
|
@@ -83,7 +81,7 @@ export default {
|
|
} else { // 否则重连
|
|
} else { // 否则重连
|
|
that.commit('websocket/reconnect')
|
|
that.commit('websocket/reconnect')
|
|
}
|
|
}
|
|
- state.serverTimeoutObj = setTimeout(function () {
|
|
|
|
|
|
+ state.serverTimeoutObj = setTimeout(() => {
|
|
// 超时关闭
|
|
// 超时关闭
|
|
state.websock.close()
|
|
state.websock.close()
|
|
}, state.timeout)
|
|
}, state.timeout)
|