| | |
| | | import { CommonUtils } from "@/utils/common.js"; |
| | | import { |
| | | CommonUtils |
| | | } from "@/utils/common.js"; |
| | | |
| | | class WebSocketServices { |
| | | constructor() { |
| | | this.wsInstance = null; // WebSocket 实例 |
| | | this.isConnecting = false; // 避免并发重连 |
| | | this.isConnected = false; // 避免并发重连 |
| | | this.wsUrl = CommonUtils.httpFormatWs() |
| | | this.wsUrl = CommonUtils.httpFormatWs() |
| | | } |
| | | |
| | | |
| | | // 建立WebSocket连接 |
| | | createConnect(userId) { // 使用用户标识 作为后端连接的凭据 |
| | | |
| | | if(this.isConnecting || !userId || this.isConnected) { |
| | | createConnect(userId, userName) { // 使用用户标识 作为后端连接的凭据 |
| | | console.log('wsUrl: ', this.wsUrl); |
| | | console.log('userId: ', userId); |
| | | console.log('userName: ', userName); |
| | | if (this.isConnecting || !userId || this.isConnected) { |
| | | return |
| | | } |
| | | console.log('wsUrl: ',this.wsUrl); |
| | | console.log('userId: ',userId); |
| | | this.wsInstance = uni.connectSocket({ |
| | | url: this.wsUrl, |
| | | header: { |
| | | "X-User-Id": userId |
| | | }, |
| | | url: this.wsUrl + `?userId=${userId}&userName=${encodeURIComponent(userName)}`, |
| | | success() { |
| | | this.isConnecting = true |
| | | } |
| | | }) |
| | | |
| | | |
| | | // 监听套接字连接建立 |
| | | uni.onSocketOpen((res) => { |
| | | console.log('[webSocket]: 套接字连接建立成功'); |
| | | this.isConnecting = false |
| | | this.isConnected = true |
| | | console.log('res: ',res); |
| | | console.log('res: ', res); |
| | | this.wsInstance = res.socketTask |
| | | }) |
| | | |
| | | uni.onSocketMessage((res) => { |
| | | let message = JSON.parse(res.data) |
| | | console.log('message: ', message); |
| | | if (message.Type == 'Message') { |
| | | // 消息信号 |
| | | let content = JSON.parse(message.Content) |
| | | console.log('content: ', content); |
| | | |
| | | this.showTaskTip(`您有${content.length}条消息需要处理!`) |
| | | } else if (message.Type == 'ping') { |
| | | // 心跳信号 |
| | | uni.sendSocketMessage({ |
| | | data: "pong" |
| | | }) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | // 重连 |
| | | reConnect(reCount = 1, limit = 3) { |
| | | if(reCount > limit) { |
| | | if (reCount > limit) { |
| | | uni.showToast({ |
| | | icon: 'none', |
| | | title: `超出最大重连次数。已退出连接` |
| | |
| | | icon: 'none', |
| | | title: `正在尝试重连,重连次数 ${reCount}` |
| | | }) |
| | | |
| | | reConnect(reCount+1, limit) |
| | | |
| | | |
| | | reConnect(reCount + 1, limit) |
| | | |
| | | uni.hideToast() |
| | | } |
| | | |
| | |
| | | disConnect() { |
| | | |
| | | } |
| | | |
| | | showTaskTip(Content) { |
| | | console.log('Content: ', Content); |
| | | // #ifdef APP-PLUS || APP |
| | | console.log('Content2: ', Content); |
| | | let content = Content; |
| | | let options = { |
| | | title: "重要通知", |
| | | cover: true, // 是否覆盖上一次的通知 |
| | | when: new Date() // 通知显示时间 |
| | | }; |
| | | // TODO 跳转到指定页 |
| | | let payload = JSON.stringify({}); |
| | | |
| | | plus.push.createMessage(content, payload, options); |
| | | // #endif |
| | | } |
| | | } |
| | | |
| | | export default new WebSocketServices() |