index-2c34a50f.js 1.2 KB

1
  1. const r="ws://"+window.location.host+"/wss",u=r;(function(){console.log(u)})();function b(){const n={host:u,ws:void 0,reconnection:!0,reconnectiontime:10,subscription:[],open:d,send:f,close:l,onmessage:void 0};function d(o){return o!=null&&o.host&&(n.host=o.host),o!=null&&o.reconnection&&(n.reconnection=o.reconnection),o!=null&&o.reconnectiontime&&(n.reconnectiontime=o.reconnectiontime),o!=null&&o.subscription&&(n.subscription=o.subscription),new Promise((t,e)=>{!n.ws&&(n.ws=new WebSocket(n.host)),n.ws&&(n.ws.onopen=function(){var i,s,w;((i=n.ws)==null?void 0:i.readyState)==WebSocket.OPEN&&(n.ws.onmessage=function(c){n.onmessage&&n.onmessage(c)}),((s=n.ws)==null?void 0:s.readyState)==WebSocket.OPEN&&(n.ws.onclose=function(c){console.log("ws连接关闭",c),n.ws=void 0,n.reconnection&&setTimeout(n.open,n.reconnectiontime*1e3)}),(w=n.subscription)!=null&&w.length&&n.send({messageType:"SUBSCRIBE",topics:n.subscription}),t(n)}),n.ws&&(n.ws.onerror=function(){n.ws=void 0,n.reconnection&&setTimeout(n.open,n.reconnectiontime*1e3),e(n)})})}function f(o){var e;!o.messageId&&(o.messageId=new Date().getTime()+"");const t=JSON.stringify(o);(e=n.ws)==null||e.send(t)}function l(){n.ws&&function(){n.reconnection=!1,n.ws.close()}()}return n}export{b as u};