Store.createWebSocket(Math.random())
url = `ws://${window.backend_server.slice(7)}/apronMapWebsocket/`
ws = null
reconnectTimeInterval = 2000
timeoutObj = null
timeout = 10000
webSocketId
@action createWebSocket = (id) => {
let url = `ws://${UI.configIP.slice(7)}/apronMapWebsocket/`
this.webSocketId = id
try {
if ('WebSocket' in window) {
this.ws = new window.WebSocket(`${url}${id}`)
} else if ('MozWebSocket' in window) {
this.ws = new window.MozWebSocket(`${url}${id}`)
} else {
this.ws = new window.SockJS(`${url}${id}`)
}
this.initEventHandle()
} catch (e) {
this.reconnect()
}
}
@action initEventHandle = () => {
this.ws.onclose = () => {
clearTimeout(this.timeoutObj)
clearTimeout(this.circlrList)
this.ifCircle = false
}
this.ws.onerror = () => {
this.reconnect()
}
this.ws.onopen = () => {
this.heartCheckStart()
}
this.ws.onmessage = (e) => {
console.log(JSON.parse(e.data)
}
}
@action reconnect = () => {
if (this.lockReconnect) return
this.lockReconnect = true
clearTimeout(this.timeoutObj)
setTimeout(() => {
this.createWebSocket(this.webSocketId)
}, this.reconnectTimeInterval)
}
@action heartCheckClose = () => {
clearTimeout(this.timeoutObj)
this.ws && this.ws.close()
}