heartCheck: { reconnectCount: 5, timeout: 50000, timeoutObj: null, serverTimeoutObj: null, reset: () => { this.heartCheck.reconnectCount = 5; if (this.heartCheck.timeoutObj) clearTimeout(this.heartCheck.timeoutObj); if (this.heartCheck.serverTimeoutObj) clearTimeout(the this .heartCheck.serverTimeoutObj); the console.log ( 'RESET' ) the this .heartCheck.timeoutObj = the setTimeout (() => { the this .websoket.send ( "Alive" ); // send a timeout in the past had not received message to disconnect and reconnect it to the this .heartCheck.serverTimeoutObj = the setTimeout (() => { the this .websoket.close (); the this .websoketFn (); // reconnection }, the this .heartCheck.timeout) }, the this.heartCheck.timeout) }, }, // data needed to define, and to disconnect and reconnect function websoketFn () { // this.websoket a WebSocket new new = (WSS `:` + // URL); the this .websoket = new new a WebSocket ( `WS: // . 192.168.10.183` the this $ + route.query.roomId) the console.log ( the this .websoket); // open callback the this .websoket.onopen = () => { the this .heartCheck .reset (); the console.log ( 'sending data' ) }; // communication the this .websoket.onmessage = (Event) =>{ // previous heartbeat resets the this .heartCheck.reset (); the console.log ( 'received data ...' ); }; the this .websoket.onerror = (Event) => { the this .heartCheck.reconnectCount- - ; IF ( the this .heartCheck.reconnectCount> 0 ) { the setTimeout (() => { the this .websoketFn (); // reconnection }, 2000 ) } the else { the console.log ('链接出错'); } }; this.websoket.onclose = () => { if (this.heartCheck.timeoutObj) clearTimeout(this.heartCheck.timeoutObj); if (this.heartCheck.serverTimeoutObj) clearTimeout(this.heartCheck.serverTimeoutObj); console.log("连接已关闭"); }; },