|
@@ -30,7 +30,11 @@ export default {
|
|
code: '',
|
|
code: '',
|
|
ws: null,
|
|
ws: null,
|
|
height: '20vh',
|
|
height: '20vh',
|
|
- winner: ''
|
|
|
|
|
|
+ winner: '',
|
|
|
|
+ heartbeat: null,
|
|
|
|
+ limitConnect: 3,
|
|
|
|
+ timeConnect: 0,
|
|
|
|
+ loading: null
|
|
}
|
|
}
|
|
},
|
|
},
|
|
watch: {
|
|
watch: {
|
|
@@ -58,6 +62,7 @@ export default {
|
|
},
|
|
},
|
|
beforeDestroy() {
|
|
beforeDestroy() {
|
|
this.ws.close()
|
|
this.ws.close()
|
|
|
|
+ clearInterval(this.heartbeat)
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
async init() {
|
|
async init() {
|
|
@@ -74,8 +79,14 @@ export default {
|
|
this.createWebsocket()
|
|
this.createWebsocket()
|
|
},
|
|
},
|
|
async getDataByCode(code) {
|
|
async getDataByCode(code) {
|
|
- const { data = [] } = await http.get('/api/list', { code })
|
|
|
|
- this.list = data
|
|
|
|
|
|
+ const loading = this.$loading()
|
|
|
|
+ try {
|
|
|
|
+ const { data = [] } = await http.get('/api/list', { code })
|
|
|
|
+ this.list = data
|
|
|
|
+ } catch (error) {
|
|
|
|
+ this.$message.error('获取数据失败')
|
|
|
|
+ }
|
|
|
|
+ loading.close()
|
|
},
|
|
},
|
|
onStatusChange(cardIndex, status) {
|
|
onStatusChange(cardIndex, status) {
|
|
this.list[cardIndex].status = status
|
|
this.list[cardIndex].status = status
|
|
@@ -88,7 +99,19 @@ export default {
|
|
const ws = new WebSocket(process.env.VUE_APP_WS_URL)
|
|
const ws = new WebSocket(process.env.VUE_APP_WS_URL)
|
|
this.ws = ws
|
|
this.ws = ws
|
|
ws.onopen = () => {
|
|
ws.onopen = () => {
|
|
|
|
+ if (this.timeConnect) {
|
|
|
|
+ this.$message.success('连接成功')
|
|
|
|
+ }
|
|
console.log('连接成功')
|
|
console.log('连接成功')
|
|
|
|
+ this.timeConnect = 0
|
|
|
|
+ this.limitConnect = 3
|
|
|
|
+ if (this.loading) {
|
|
|
|
+ this.loading.close()
|
|
|
|
+ this.loading = null
|
|
|
|
+ }
|
|
|
|
+ this.heartbeat = setInterval(() => {
|
|
|
|
+ ws.send('')
|
|
|
|
+ }, 5000)
|
|
ws.send(JSON.stringify({ status: 'connect' }))
|
|
ws.send(JSON.stringify({ status: 'connect' }))
|
|
}
|
|
}
|
|
ws.onmessage = e => {
|
|
ws.onmessage = e => {
|
|
@@ -99,11 +122,38 @@ export default {
|
|
this.list = list
|
|
this.list = list
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error(error)
|
|
console.error(error)
|
|
|
|
+ this.$message.error('websocket获取信息失败')
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ws.onerror = e => {
|
|
|
|
+ console.error(e)
|
|
|
|
+ if (this.timeConnect === 3) {
|
|
|
|
+ this.$message.error('与服务器失去连接,请重新刷新页面')
|
|
|
|
+ this.loading.close()
|
|
|
|
+ this.loading = null
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- ws.onerror = e => console.error(e)
|
|
|
|
ws.onclose = () => {
|
|
ws.onclose = () => {
|
|
console.log('connection closed')
|
|
console.log('connection closed')
|
|
|
|
+ if (this.timeConnect === 0) {
|
|
|
|
+ this.$message.error('与服务器断开连接,正在重新连接...')
|
|
|
|
+ this.loading = this.$loading()
|
|
|
|
+ }
|
|
|
|
+ clearInterval(this.heartbeat)
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ this.reconnect()
|
|
|
|
+ }, 1000)
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ reconnect() {
|
|
|
|
+ const { limitConnect } = this
|
|
|
|
+ if (limitConnect > 0) {
|
|
|
|
+ this.limitConnect -= 1
|
|
|
|
+ this.timeConnect += 1
|
|
|
|
+ this.$message.error(`尝试第${this.timeConnect}次重连...`)
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ this.createWebsocket()
|
|
|
|
+ }, 10e3)
|
|
}
|
|
}
|
|
},
|
|
},
|
|
isOverHandle() {
|
|
isOverHandle() {
|