var e=Object.defineProperty,t=(t,n,s)=>(((t,n,s)=>{n in t?e(t,n,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[n]=s})(t,"symbol"!=typeof n?n+"":n,s),s);import{at as n,y as s}from"./vue.js?v=1723125373998";import{n as o,j as c,cz as a}from"./index.js?v=1723125373998";import{i}from"./data.js?v=1723125373998";class r{constructor(e){t(this,"socket",null),t(this,"url",""),t(this,"connectURL",(()=>{const{host:e,protocol:t}=window.location;return("http:"===t?"ws://":"wss://")+e+"/v2"})()),t(this,"sendData",{}),this.url=e.url,this.connectURL+=e.url,this.init(e)}init(e){"WebSocket"in window?this.socket=n(this.connectURL,{autoReconnect:{retries:3,delay:500},onConnected:t=>{e.onConnected&&e.onConnected(t),this.onConnected(t,e)},onMessage:(t,n)=>{e.onMessage&&e.onMessage(t,n),this.onMessage(t,n)},onDisconnected:(t,n)=>{e.onDisconnected&&e.onDisconnected(t,n),this.onDisconnected()},onError:(t,n)=>{e.onError&&e.onError(t,n),this.onError()}}):o.error("The browser does not support Websocket")}isStatus(e){var t,n;return((null==(t=this.socket)?void 0:t.status.value)||(null==(n=this.socket)?void 0:n.status))===e}channelVerify(){var e;const t={};t["x-http-token"]=null==(e=document.getElementById("request_token_head"))?void 0:e.getAttribute("token"),this.send(t)}send(e){var t;if(this.isStatus("OPEN"))null==(t=this.socket)||t.send(c(e)?JSON.stringify(e):e);else{const t=a(10);this.sendData[t]={data:e,status:!1,request:!1,callback:()=>{}}}}onConnected(e,t){t.noInit||this.channelVerify(),this.onSendData()}onSendData(){Object.entries(this.sendData).forEach((([e,t])=>{this.send(t.data),delete this.sendData[e]}))}onMessage(e,t){if("/ws_home"===this.url||"/ws_model"===this.url)try{if(i(t.data)){const n=JSON.parse(t.data),s=n.callback||n.ws_callback;s&&this.sendData[s]&&this.sendData[s].callback(e,t)}}catch(n){console.log(n)}}onDisconnected(){this.close(),console.log("断开连接")}onError(){console.log("连接错误")}close(){var e;null==(e=this.socket)||e.close()}}function l({url:e,onMessage:t,onDisconnected:n}){let o=null;const c=()=>{o&&(o.close(),o=null)};return s((()=>{c()})),{getSocket:()=>o,sendSocket:e=>{null==o||o.send(e)},closeWebSocket:c,createWebSocket:()=>{var s;s={url:e,onMessage:(e,n)=>{t(e,n)},onDisconnected:()=>{o=null,null==n||n()}},o=new r(s)}}}export{l as u};
