pages-API-websocket-socketTask-websocket-socketTask.DlhPjl9E.js 2.4 KB

1
  1. import{_ as s,a3 as e,ar as t,aa as o,as as c,at as a,G as n,b as i,d as l,p as k,w as h,H as d,i as r,o as g,f as u,k as p,t as f,aw as m,ax as T,R as w}from"./index-2PZN6pGH.js";let _=e().platform;const C=s({data:()=>({connected:!1,connecting:!1,socketTask:!1,msg:!1}),computed:{showMsg(){return this.connected?this.msg?"收到消息:"+this.msg:"等待接收消息":"尚未连接"}},onUnload(){t(),this.socketTask&&this.socketTask.close&&this.socketTask.close()},methods:{connect(){if(this.connected||this.connecting)return o({content:"正在连接或者已经连接,请勿重复连接",showCancel:!1}),!1;this.connecting=!0,c({title:"连接中..."}),this.socketTask=a({url:"wss://echo.websocket.org",data:()=>({msg:"Hello"}),success(s){},fail(s){}}),console.log(this.socketTask),this.socketTask.onOpen((s=>{this.connecting=!1,this.connected=!0,t(),n({icon:"none",title:"连接成功"}),console.log("onOpen",s)})),this.socketTask.onError((s=>{this.connecting=!1,this.connected=!1,t(),o({content:"连接失败,可能是websocket服务不可用,请稍后再试",showCancel:!1}),console.log("onError",s)})),this.socketTask.onMessage((s=>{this.msg=s.data,console.log("onMessage",s)})),this.socketTask.onClose((s=>{this.connected=!1,this.startRecive=!1,this.socketTask=!1,this.msg=!1,console.log("onClose",s)})),console.log("task",this.socketTask)},send(){this.socketTask.send({data:"from "+_+" : "+parseInt(1e4*Math.random()).toString(),success(s){console.log(s)},fail(s){console.log(s)}})},close(){this.socketTask&&this.socketTask.close&&this.socketTask.close()}}},[["render",function(s,e,t,o,c,a){const n=i(l("page-head"),d),_=r,C=w;return g(),k(_,null,{default:h((()=>[u(n,{title:"websocket通讯示例"}),u(_,{class:"uni-padding-wrap"},{default:h((()=>[u(_,{class:"uni-btn-v"},{default:h((()=>[u(_,{class:"websocket-msg"},{default:h((()=>[p(f(a.showMsg),1)])),_:1}),u(C,{type:"primary",onClick:a.connect},{default:h((()=>[p("连接websocket服务")])),_:1},8,["onClick"]),m(u(C,{type:"primary",onClick:a.send},{default:h((()=>[p("发送一条消息")])),_:1},8,["onClick"]),[[T,c.connected]]),u(C,{type:"primary",onClick:a.close},{default:h((()=>[p("断开websocket服务")])),_:1},8,["onClick"]),u(_,{class:"websocket-tips"},{default:h((()=>[p("发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)")])),_:1})])),_:1})])),_:1})])),_:1})}],["__scopeId","data-v-90e828af"]]);export{C as default};