acdr-ui/dist/build/mp-weixin/service/webSocket.js

2 lines
442 B
JavaScript

"use strict";const e=require("../store/user.js");exports.useWebSocket=function(){const o=e.useUserStore();let s=null;return{connectWebSocket:()=>{if(!o.isLogined)return;const e=o.userInfo.token;s||(s=new WebSocket(`ws://47.99.70.12:28184/api/chat/${e}`),s.onopen=()=>{},s.onmessage=e=>{e.data},s.onerror=e=>{},s.onclose=()=>{s=null})},sendMessage:e=>{s&&s.readyState===WebSocket.OPEN&&s.send(e)},closeWebSocket:()=>{s&&(s.close(),s=null)}}};