im-ui-mobile 0.0.47 → 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,248 +0,0 @@
1
- // 类型定义
2
- interface WebSocketMessage {
3
- cmd: number;
4
- data: any;
5
- }
6
-
7
- interface LoginInfo extends WebSocketMessage {
8
- cmd: 0;
9
- data: {
10
- accessToken: string;
11
- };
12
- }
13
-
14
- interface HeartBeat extends WebSocketMessage {
15
- cmd: 1;
16
- data: {};
17
- }
18
-
19
- type MessageCallback = (cmd: number, data: any) => void;
20
- type CloseCallback = (res: any) => void;
21
- type ConnectCallback = () => void;
22
-
23
- class WebSocketManager {
24
- // 私有属性
25
- // private accessToken: string = "";
26
- private messageCallBack: MessageCallback | null = null;
27
- private closeCallBack: CloseCallback | null = null;
28
- private connectCallBack: ConnectCallback | null = null;
29
- private isConnect: boolean = false;
30
- private reconnectTimer: number | null = null;
31
- private lastConnectTime: Date = new Date();
32
- private socketTask: UniApp.SocketTask | null = null;
33
-
34
- // 心跳配置
35
- private heartCheckTimeout: number = 20000;
36
- private heartCheckTimer: number | null = null;
37
-
38
- // 私有方法 - 心跳相关
39
- private startHeartCheck(): void {
40
- if (!this.isConnect || !this.socketTask) return;
41
-
42
- // console.log('发送WebSocket心跳');
43
- const heartBeat: HeartBeat = {
44
- cmd: 1,
45
- data: {}
46
- };
47
- this.sendMessage(JSON.stringify(heartBeat));
48
- }
49
-
50
- private resetHeartCheck(): void {
51
- this.clearHeartCheck();
52
- this.heartCheckTimer = setTimeout(() => {
53
- this.startHeartCheck();
54
- }, this.heartCheckTimeout);
55
- }
56
-
57
- private clearHeartCheck(): void {
58
- if (this.heartCheckTimer) {
59
- clearTimeout(this.heartCheckTimer);
60
- this.heartCheckTimer = null;
61
- }
62
- }
63
-
64
- // 连接 WebSocket
65
- public connect(wsurl: string, token: string): void {
66
- if (this.isConnect) return;
67
-
68
- // this.accessToken = token;
69
- this.lastConnectTime = new Date();
70
-
71
- this.socketTask = uni.connectSocket({
72
- url: wsurl,
73
- fail: (e) => {
74
- console.error("WebSocket连接失败:", e);
75
- console.log("10秒后重连...");
76
- this.scheduleReconnect(wsurl, token);
77
- }
78
- });
79
-
80
- this.setupSocketEvents(token);
81
- }
82
-
83
- // 设置 WebSocket 事件监听
84
- private setupSocketEvents(token: string): void {
85
- if (!this.socketTask) return;
86
-
87
- this.socketTask.onOpen(() => {
88
- // console.log("WebSocket连接成功");
89
- this.isConnect = true;
90
-
91
- // 发送登录命令
92
- const loginInfo: LoginInfo = {
93
- cmd: 0,
94
- data: { accessToken: token }
95
- };
96
- this.sendMessage(JSON.stringify(loginInfo));
97
- });
98
-
99
- this.socketTask.onMessage((res) => {
100
- try {
101
- const message: WebSocketMessage = JSON.parse(res.data);
102
- this.handleMessage(message);
103
- } catch (error) {
104
- console.error("消息解析失败:", error);
105
- }
106
- });
107
-
108
- this.socketTask.onClose((res) => {
109
- // console.log('WebSocket连接关闭', res);
110
- this.handleClose(res);
111
- });
112
-
113
- this.socketTask.onError((e) => {
114
- console.error("WebSocket错误:", e);
115
- this.handleClose(e);
116
- });
117
- }
118
-
119
- // 处理接收到的消息
120
- private handleMessage(message: WebSocketMessage): void {
121
- switch (message.cmd) {
122
- case 0: // 登录成功
123
- this.resetHeartCheck();
124
- this.connectCallBack?.();
125
- // console.log('WebSocket登录成功');
126
- break;
127
- case 1: // 心跳响应
128
- this.resetHeartCheck();
129
- break;
130
- default: // 其他消息
131
- // console.log("接收到消息", message);
132
- this.messageCallBack?.(message.cmd, message.data);
133
- break;
134
- }
135
- }
136
-
137
- // 处理连接关闭
138
- private handleClose(res: any): void {
139
- this.isConnect = false;
140
- this.clearHeartCheck();
141
- this.closeCallBack?.(res);
142
- }
143
-
144
- // 重新连接
145
- public reconnect(wsurl: string, token: string): void {
146
- console.log("尝试重新连接");
147
- if (this.isConnect) return;
148
-
149
- const timeDiff: number = new Date().getTime() - this.lastConnectTime.getTime();
150
- const delay: number = timeDiff < 10000 ? 10000 - timeDiff : 0;
151
-
152
- if (this.reconnectTimer) {
153
- clearTimeout(this.reconnectTimer);
154
- }
155
-
156
- this.reconnectTimer = setTimeout(() => {
157
- this.connect(wsurl, token);
158
- }, delay);
159
- }
160
-
161
- // 安排重新连接
162
- private scheduleReconnect(wsurl: string, token: string): void {
163
- setTimeout(() => {
164
- this.reconnect(wsurl, token);
165
- }, 10000);
166
- }
167
-
168
- // 关闭连接
169
- public close(code: number = 1000): void {
170
- if (!this.isConnect || !this.socketTask) return;
171
-
172
- this.clearHeartCheck();
173
-
174
- this.socketTask.close({
175
- code: code,
176
- complete: () => {
177
- this.isConnect = false;
178
- // console.log("关闭WebSocket连接");
179
- },
180
- fail: (e) => {
181
- console.error("关闭WebSocket连接失败", e);
182
- }
183
- });
184
- }
185
-
186
- // 发送消息
187
- public sendMessage(message: string): void {
188
- if (!this.isConnect || !this.socketTask) {
189
- console.warn("WebSocket未连接,无法发送消息");
190
- return;
191
- }
192
-
193
- this.socketTask.send({ data: message });
194
- }
195
-
196
- // 注册连接成功回调
197
- public onConnect(callback: ConnectCallback): void {
198
- this.connectCallBack = callback;
199
- }
200
-
201
- // 注册消息接收回调
202
- public onMessage(callback: MessageCallback): void {
203
- this.messageCallBack = callback;
204
- }
205
-
206
- // 注册连接关闭回调
207
- public onClose(callback: CloseCallback): void {
208
- this.closeCallBack = callback;
209
- }
210
-
211
- // 获取连接状态
212
- public getIsConnect(): boolean {
213
- return this.isConnect;
214
- }
215
-
216
- // 获取最后连接时间
217
- public getLastConnectTime(): Date {
218
- return this.lastConnectTime;
219
- }
220
-
221
- // 设置心跳间隔
222
- public setHeartCheckTimeout(timeout: number): void {
223
- this.heartCheckTimeout = timeout;
224
- }
225
-
226
- // 销毁实例
227
- public destroy(): void {
228
- this.close();
229
- this.clearHeartCheck();
230
-
231
- if (this.reconnectTimer) {
232
- clearTimeout(this.reconnectTimer);
233
- this.reconnectTimer = null;
234
- }
235
-
236
- this.messageCallBack = null;
237
- this.closeCallBack = null;
238
- this.connectCallBack = null;
239
- this.socketTask = null;
240
- }
241
- }
242
-
243
- // 创建单例实例
244
- const webSocketManager = new WebSocketManager();
245
-
246
- // 导出单例和类
247
- export { webSocketManager, WebSocketManager };
248
- export default webSocketManager;