123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- export default {
- data() {
- return {
-
- webSocket: null,
- webSocketTaskData: {},
- showVehivlesTaskId: null,
- socketReconnectNum: 0,
- lockReconnect: false,
- connectSocketUrl: '',
- connectSocketName: '',
-
- setHearTime: 10000,
- heartTimeOut: 20000,
- timeoutObj: null
- }
- },
- methods: {
-
- heartReset() {
- this.timeoutObj && clearTimeout(this.timeoutObj)
-
- },
- heartStart() {
- this.timeoutObj && clearTimeout(this.timeoutObj)
- this.timeoutObj = setInterval(() => {
-
- if (this.webSocket.readyState !== 1) {
-
- console.log('重新连接', this.socketReconnectNum)
- this.socketReconnect()
- } else {
- this.webSocket.send('heartCheck' + this.connectSocketName)
- }
- }, 10000)
- },
-
- async socketReconnect() {
-
-
- if (this.socketReconnectNum > 5) {
- this.heartReset()
- } else {
-
- this.socketReconnectNum++
- this.socketConnectMixin()
- }
- },
- resetSocketData() {
- this.socketReconnectNum = 0
-
- },
-
- socketConnectMixin() {
-
- return new Promise((resolve) => {
- if (typeof WebSocket === 'undefined') {
- console.log('遗憾:您的浏览器不支持WebSocket')
- } else {
- console.log(' new WebSocket', this.connectSocketUrl)
- this.webSocket = new WebSocket(this.connectSocketUrl)
-
- this.webSocket.onopen = () => {
- this.heartStart()
- resolve(true)
- }
-
- this.webSocket.onmessage = (msg) => {
- this.heartStart()
- this.resetSocketData()
- this.webSocketMessage(msg)
- }
-
- this.webSocket.onclose = () => {
-
-
-
-
- console.log('Socket已关闭', this.connectSocketName)
- }
-
- this.webSocket.onerror = () => {
- console.log('Socket发生了错误', this.connectSocketName)
-
-
- }
- }
- })
- },
-
- webSocketMessage() {},
-
- webSocketClose() {
- this.webSocket.close()
- this.heartReset()
- }
- }
- }
|