wtt
2026-01-19 4e2724cb51fe4247d9c28cdf54b45337b057da73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
    CommonUtils
} from "@/utils/common.js";
 
class WebSocketServices {
    constructor() {
        this.wsInstance = null; // WebSocket 实例
        this.isConnecting = false; // 避免并发重连
        this.isConnected = false; // 避免并发重连
        this.wsUrl = CommonUtils.httpFormatWs()
    }
 
    // 建立WebSocket连接
    createConnect(userId, userName) { // 使用用户标识 作为后端连接的凭据
        console.log('wsUrl: ', this.wsUrl);
        console.log('userId: ', userId);
        console.log('userName: ', userName);
        if (this.isConnecting || !userId || this.isConnected) {
            return
        }
        this.wsInstance = uni.connectSocket({
            url: this.wsUrl + `?userId=${encodeURIComponent(userId)}&userName=${encodeURIComponent(userName)}`,
            success() {
                this.isConnecting = true
            }
        })
 
        // 监听套接字连接建立
        uni.onSocketOpen((res) => {
            console.log('[webSocket]: 套接字连接建立成功');
            this.isConnecting = false
            this.isConnected = true
            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) {
            uni.showToast({
                icon: 'none',
                title: `超出最大重连次数。已退出连接`
            })
            this.isConnecting = false
            return
        }
        uni.showToast({
            icon: 'none',
            title: `正在尝试重连,重连次数 ${reCount}`
        })
 
        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()