import{E as n,L as i,ag as e}from"./index-DWRYsVdq.js";const a="4.0.0-alpha.1",r=2e3;class h extends n{constructor(t){super(),this.ws=null,this.reconnectAttempts=0,this.reconnectTimer=null,this.heartbeatTimer=null,this._state="disconnected",this._disposed=!1,this.manualClose=!1,this.logger=i.create("WebSocket"),this.options={url:t.url,protocols:t.protocols??[],autoReconnect:t.autoReconnect??!0,reconnectDelay:t.reconnectDelay??3e3,maxReconnectAttempts:t.maxReconnectAttempts??10,heartbeatInterval:t.heartbeatInterval??r,heartbeatMessage:t.heartbeatMessage??"ping",binaryType:t.binaryType??"arraybuffer"}}get disposed(){return this._disposed}get state(){return this._state}get connected(){return this._state==="connected"}connect(){if(!(this._disposed||this._state==="connected"||this._state==="connecting")){this.manualClose=!1,this._state="connecting",this.emit("connecting",void 0);try{this.ws=new WebSocket(this.options.url,this.options.protocols),this.ws.binaryType=this.options.binaryType,this.setupEventHandlers()}catch(t){this.emit("error",t),this.scheduleReconnect()}}}setupEventHandlers(){this.ws&&(this.ws.onopen=()=>{this._state="connected",this.reconnectAttempts=0,this.startHeartbeat(),this.emit("connected",void 0),this.logger.info("Connected")},this.ws.onclose=t=>{this.stopHeartbeat(),this._state="disconnected",this.emit("disconnected",{code:t.code,reason:t.reason}),this.logger.info("Disconnected",{code:t.code,reason:t.reason}),!this.manualClose&&this.options.autoReconnect&&this.scheduleReconnect()},this.ws.onerror=t=>{this.emit("error",new Error("WebSocket error")),this.logger.error("Error occurred")},this.ws.onmessage=t=>{this.emit("message",{data:t.data})})}send(t){if(!this.ws||this._state!=="connected")return this.logger.warn("Cannot send: not connected"),!1;try{return this.ws.send(t),!0}catch(s){return this.logger.error("Send failed",s),!1}}sendJSON(t){return this.send(JSON.stringify(t))}disconnect(t=1e3,s=""){this.manualClose=!0,this.stopHeartbeat(),this.cancelReconnect(),this.ws&&(this.ws.close(t,s),this.ws=null),this._state="disconnected"}startHeartbeat(){this.options.heartbeatInterval<=0||(this.heartbeatTimer=e.instance.setInterval(()=>{if(this._state==="connected"){const t=typeof this.options.heartbeatMessage=="function"?this.options.heartbeatMessage():this.options.heartbeatMessage;this.send(t)}},this.options.heartbeatInterval))}stopHeartbeat(){this.heartbeatTimer!==null&&(e.instance.clearInterval(this.heartbeatTimer),this.heartbeatTimer=null)}scheduleReconnect(){if(!(this._disposed||this.manualClose)){if(this.reconnectAttempts>=this.options.maxReconnectAttempts){this.logger.warn("Max reconnect attempts reached");return}this.reconnectAttempts++,this._state="reconnecting",this.emit("reconnecting",{attempt:this.reconnectAttempts}),this.logger.info(`Reconnecting in ${this.options.reconnectDelay}ms (attempt ${this.reconnectAttempts})`),this.reconnectTimer=e.instance.setTimeout(()=>{this.connect()},this.options.reconnectDelay)}}cancelReconnect(){this.reconnectTimer!==null&&(e.instance.clearTimeout(this.reconnectTimer),this.reconnectTimer=null)}setUrl(t){this.options.url=t}dispose(){this._disposed||(this._disposed=!0,this.disconnect(),this.removeAllListeners(),this.logger.debug("Disposed"))}}export{r as D,a as S,h as W};
