"use strict";(self["webpackChunkblivechat"]=self["webpackChunkblivechat"]||[]).push([[435],{2435:function(e,t,s){s.r(t),s.d(t,{default:function(){return T}});var i=s(7389),o=s(8697),n=s(8310);const a=0,r=1,h=2,l=3,c=4,d=5,u=6,m=7,w=8,b=1,g=15e3;class T{constructor(e,t){this.roomKey=e,this.autoTranslate=t,this.msgHandler=o.wR(),this.websocket=null,this.retryCount=0,this.totalRetryCount=0,this.isDestroying=!1,this.receiveTimeoutTimerId=null}start(){this.wsConnect()}stop(){this.isDestroying=!0,this.websocket&&this.websocket.close()}addDebugMsg(e){this.msgHandler.onDebugMsg(new n.EN({content:e}))}async wsConnect(){if(this.isDestroying)return;this.addDebugMsg("Connecting"),await(0,i.Yy)();let e=(0,i.SV)();if(null===e)return this.addDebugMsg("No available endpoint"),void window.setTimeout((()=>this.onWsClose()),0);let t=e.replace(/^http(s?):/,"ws$1:");t+="/api/chat",this.websocket=new WebSocket(t),this.websocket.onopen=this.onWsOpen.bind(this),this.websocket.onclose=this.onWsClose.bind(this),this.websocket.onmessage=this.onWsMessage.bind(this)}onWsOpen(){this.addDebugMsg("Connected and authenticating"),this.websocket.send(JSON.stringify({cmd:r,data:{roomKey:this.roomKey,config:{autoTranslate:this.autoTranslate}}})),this.refreshReceiveTimeoutTimer()}refreshReceiveTimeoutTimer(){this.receiveTimeoutTimerId&&window.clearTimeout(this.receiveTimeoutTimerId),this.receiveTimeoutTimerId=window.setTimeout(this.onReceiveTimeout.bind(this),g)}onReceiveTimeout(){this.receiveTimeoutTimerId=null,console.warn("接收消息超时"),this.addDebugMsg("Receiving message timed out"),this.websocket&&(this.websocket.onclose&&window.setTimeout((()=>this.onWsClose()),0),this.websocket.onopen=this.websocket.onclose=this.websocket.onmessage=null,this.websocket.close())}onWsClose(){if(this.addDebugMsg("Disconnected"),this.websocket=null,this.receiveTimeoutTimerId&&(window.clearTimeout(this.receiveTimeoutTimerId),this.receiveTimeoutTimerId=null),!this.isDestroying)if(this.retryCount++,this.totalRetryCount++,console.warn(`掉线重连中 retryCount=${this.retryCount}, totalRetryCount=${this.totalRetryCount}`),this.totalRetryCount>30){this.stop();let e=new n.yr(n.Lx,"The connection has lost too many times");this.msgHandler.onFatalError(e)}else this.addDebugMsg("Scheduling reconnection"),window.setTimeout(this.wsConnect.bind(this),this.getReconnectInterval())}getReconnectInterval(){let e=Math.min(1e3+2e3*(this.totalRetryCount-1),2e4);return e+=3e3*Math.random(),e}onWsMessage(e){let{cmd:t,data:s}=JSON.parse(e.data);switch(t){case a:this.refreshReceiveTimeoutTimer(),this.websocket.send(JSON.stringify({cmd:a}));break;case h:{let e=null,t=s[13],i=s[14];t===b&&(e=i[0]);let a=s[4];s=new n.rf({avatarUrl:s[0],timestamp:s[1],authorName:s[2],authorType:s[3],content:a,privilegeType:s[5],isGiftDanmaku:Boolean(s[6])||o.e0(a),authorLevel:s[7],isNewbie:Boolean(s[8]),isMobileVerified:Boolean(s[9]),medalLevel:s[10],id:s[11],translation:s[12],emoticon:e,isMirror:Boolean(s[18]),uid:s[16],medalName:s[17]}),this.msgHandler.onAddText(s);break}case l:s=new n.He(s),this.msgHandler.onAddGift(s);break;case c:s=new n.DZ(s),this.msgHandler.onAddMember(s);break;case d:s=new n._R(s),this.msgHandler.onAddSuperChat(s);break;case u:s=new n.EP(s),this.msgHandler.onDelSuperChat(s);break;case m:s=new n.El({id:s[0],translation:s[1]}),this.msgHandler.onUpdateTranslation(s);break;case w:{this.stop();let e=new n.yr(s.type,s.msg);this.msgHandler.onFatalError(e);break}}t!==w&&(this.retryCount=0)}}}}]);
//# sourceMappingURL=435.97d68071.js.map