UNPKG

yunzai-micro-plugin

Version:

Yunzai开发管理面板

2 lines (1 loc) 1.45 kB
var o=Object.defineProperty;var a=(r,s,t)=>s in r?o(r,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[s]=t;var i=(r,s,t)=>(a(r,typeof s!="symbol"?s+"":s,t),t);import{aj as h,q as l}from"./index-6f2f2fc7.js";const n=l();class u{constructor(s="/micro/webui/chat"){i(this,"ws");i(this,"url");i(this,"msgQueue");i(this,"clientId");i(this,"reConnectSum");i(this,"address");this.address={publicAddress:"",privateAddress:""},this.ws=null,this.url=s,this.msgQueue=[],this.clientId="",this.reConnectSum=0}async openWs(s="",t=null){const d=new URL(n.originAddress);this.ws=new WebSocket((s||`ws://${d.hostname}:${n.originPort?n.originPort:d.port}`)+this.url),this.ws.addEventListener("open",e=>{console.log("ws连接成功!"+e.target),t&&t()}),this.ws.addEventListener("message",e=>{this.msgQueue.push(JSON.parse(e.data)),e.data.action=="meta"&&(this.clientId=e.data.params)}),this.ws.addEventListener("close",e=>{console.log("ws服务端关闭!"+JSON.stringify(e)),this.clientId=""}),this.ws.addEventListener("error",async e=>{console.log("ws出错!"+JSON.stringify(e)),!this.address.privateAddress&&!this.address.publicAddress&&await this.getServerPort(),await this.openWs(this.address.privateAddress)})}async getServerPort(){let s=await h();s.code==200&&(this.address=s.data)}closeWs(){this.ws&&this.ws.close(),console.log("ws关闭成功!")}sendWs(s){this.ws&&this.ws.send(this.clientId?{...s,ClientId:this.clientId}:s)}}export{u as W};