数据中需要这些参数data(){return{websock:null,//建立连接lockReconnect:false,//是否真正建立连接timeout:28*1000,//每30秒心跳一次timeoutObj:null,//外层心跳倒计时serverTimeoutObj:null,//内层心跳检测timeoutnum:null//断线重连倒计时};},inmethods:initWebSocket(){//初始化weosocketconstwsuri="ws://"+"xxxxx"+":portnumber";this.websock=newWebSocket(wsuri);this.websock.onopen=this.websocketonopen;this.websock.onmessage=this.websocketonmessage;this.websock.onerror=this.websocketonerror;this.websock.onclose=this.websocketclose;},reconnect(){//重新连接varthat=this;if(that.lockReconnect){//连接是否真正建立return;}that.lockReconnect=true;//不会建立连接保持重连,设置延迟以避免请求过多that.timeoutnum&&clearTimeout(that.timeoutnum);//如果此处断开重连倒计时还有值,则清除that.timeoutnum=setTimeout(function(){//然后新建连接that.initWebSocket();那.lockReconnect=false;},5000);},reset(){//重置心跳varthat=this;//清除时间(清除内外心跳时序)clearTimeout(that.timeoutObj);clearTimeout(that.serverTimeoutObj);//重启心跳that.start();},start(){//启动心跳varself=this;self.timeoutObj&&clearTimeout(self.timeoutObj);//清除外层心跳倒计时是否存在Deleteself.serverTimeoutObj&&clearTimeout(self.serverTimeoutObj);//如果内部心跳检测倒计时存在,则清除self.timeoutObj=setTimeout(function(){//重新分配并重新发送心跳检测//这里发送心跳,后端收到心跳消息后返回,if(self.websock.readyState==1){//如果连接正常//self.websock.send("heartCheck");}else{//否则重新连接self.reconnect();}self.serverTimeoutObj=setTimeout(function(){//每三秒检测一次心跳,如果某个值3秒没有响应,则关闭连接//超时并关闭self.websock.close();},self.timeout);},self.暂停);//3s一次},连接状态判断:websocketonopen(e){//连接建立后,执行send方法发送数据console.log("success");让行动={测试:“12345”};//this.websocketsend(JSON.stringify(actions));},websocketonerror(){//连接建立失败重连console.log("failure");这个.initWebSocket();},websocketonmessage(e){//数据接收constredata=JSON.parse(e.data);控制台日志(重新数据);this.aa=[...this.aa,redata.type];这个。重置();},websocketsend(Data){//发送数据this.websock.发送(数据);},websocketclose(e){//关闭console.log("Disconnect",e);}完整代码:initWebSocket(){//初始化weosocketconstwsuri="ws://"+"xxxxx"+":portnumber";this.websock=newWebSocket(wsuri);this.websock.onopen=this.websocketonopen;this.websock.onmessage=this.websocketonmessage;this.websock.onerror=this.websocketonerror;this.websock.onclose=this.websocketclose;},reconnect(){//重新连接那个=这个;if(that.lockReconnect){//连接是否真正建立return;}that.lockReconnect=true;//如果连接不上,会一直重连,设置延迟,避免请求过多that.timeoutnum&&clearTimeout(that.timeoutnum);//如果断线重连倒计时还有值,则清除that.timeoutnum=setTimeout(function(){//然后重新建立连接that.initWebSocket();that.lockReconnect=false;},5000);},reset(){//重置心跳varthat=this;//清除时间(清除内、外心跳定时器)clearTimeout(that.timeoutObj);clearTimeout(that.serverTimeoutObj);//重启心跳that.start();},start(){//启动心跳varself=this;self.timeoutObj&&clearTimeout(self.timeoutObj);//如果存在外层心跳倒计时,则清除self.serverTimeoutObj&&clearTimeout(self.serverTimeoutObj);//如果内部心跳检测倒计时存在,则清空self.timeoutObj=setTimeout(function(){//重新赋值并重新发送心跳检测//这里发送一个心跳,后端收到后,返回一个心跳消息,if(self.websock.readyState==1){//如果连接正常//self.websock.send("heartCheck");}else{//否则重连self.reconnect();}self.serverTimeoutObj=setTimeout(function(){//在三秒心跳检测中,如果某个值3秒没有响应,则关闭连接//超时关闭self.websock.close();},自超时);},自我超时);//3s一次},websocketonopen(e){//连接建立后,执行send方法发送数据console.log("success");让行动={测试:“12345”};//this.websocketsend(JSON.stringify(actions));},websocketonerror(){//连接失败重连console.log("failure");这个.initWebSocket();},websocketonmessage(e){//接收到的数据constredata=JSON.parse(e.data);控制台日志(重新数据);this.aa=[...this.aa,redata.type];这个。重置();},websocketsend(Data){//发送数据this.websock.send(Data);},websocketclose(e){//关闭console.log("Disconnect",e);}
