@jetlinks-web/core 2.1.7 → 2.1.8
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.
- package/package.json +2 -2
- package/src/axios.ts +1 -1
- package/src/websocket.ts +103 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jetlinks-web/core",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.8",
|
|
4
4
|
"main": "index.ts",
|
|
5
5
|
"module": "index.ts",
|
|
6
6
|
"keywords": [
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"axios": "^1.7.4",
|
|
23
23
|
"rxjs": "^7.8.1",
|
|
24
|
-
"@jetlinks-web/types": "^1.0.2",
|
|
25
24
|
"@jetlinks-web/constants": "^1.0.9",
|
|
25
|
+
"@jetlinks-web/types": "^1.0.2",
|
|
26
26
|
"@jetlinks-web/utils": "^1.2.8"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
package/src/axios.ts
CHANGED
|
@@ -47,7 +47,6 @@ interface RequestOptions {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
let instance: AxiosInstance
|
|
50
|
-
|
|
51
50
|
let _options: Options = {
|
|
52
51
|
filter_url: [],
|
|
53
52
|
code: 200,
|
|
@@ -61,6 +60,7 @@ let _options: Options = {
|
|
|
61
60
|
tokenExpiration: () => {},
|
|
62
61
|
}
|
|
63
62
|
|
|
63
|
+
const isApp = (window as any).__MICRO_APP_ENVIRONMENT__
|
|
64
64
|
const controller = new AbortController();
|
|
65
65
|
|
|
66
66
|
const handleRequest = (config: InternalAxiosRequestConfig) => {
|
package/src/websocket.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { wsClient } from '@/utils/websocket';
|
|
1
2
|
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
|
|
2
3
|
import { Observable, Subject, timer, Subscription, EMPTY } from 'rxjs';
|
|
3
4
|
import { retry, catchError } from 'rxjs/operators';
|
|
4
5
|
import { notification } from 'ant-design-vue';
|
|
6
|
+
import app from '@micro-zoe/micro-app'
|
|
7
|
+
import { log } from 'console';
|
|
5
8
|
|
|
6
9
|
interface WebSocketMessage {
|
|
7
10
|
type: string;
|
|
@@ -14,24 +17,32 @@ interface WebSocketMessage {
|
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
type WS_Options = {
|
|
17
|
-
|
|
20
|
+
onError?: (message: WebSocketMessage) => void
|
|
18
21
|
}
|
|
19
22
|
|
|
23
|
+
const isApp = (window as any).__MICRO_APP_ENVIRONMENT__
|
|
24
|
+
|
|
20
25
|
export class WebSocketClient {
|
|
21
26
|
private ws: WebSocketSubject<WebSocketMessage> | null = null;
|
|
22
27
|
private subscriptions = new Map<string, Subject<WebSocketMessage>>();
|
|
23
28
|
private pendingSubscriptions = new Map<string, Subject<WebSocketMessage>>();
|
|
24
29
|
private heartbeatSubscription: Subscription | null = null;
|
|
25
30
|
private reconnectAttempts = 0;
|
|
26
|
-
private readonly maxReconnectAttempts =
|
|
31
|
+
private readonly maxReconnectAttempts = 2;
|
|
27
32
|
private isConnected = false;
|
|
28
33
|
private tempQueue: WebSocketMessage[] = []; // 缓存消息队列
|
|
29
34
|
private url: string = '';
|
|
30
35
|
private options: WS_Options = {}
|
|
36
|
+
private wsClient: WebSocketClient | undefined
|
|
31
37
|
|
|
32
38
|
constructor(options?: WS_Options) {
|
|
33
39
|
this.options = options || {};
|
|
34
40
|
this.setupConnectionMonitor();
|
|
41
|
+
if (isApp) {
|
|
42
|
+
(window as any).microApp.addGlobalDataListener((data) => {
|
|
43
|
+
this.wsClient = data.wsClient
|
|
44
|
+
})
|
|
45
|
+
}
|
|
35
46
|
}
|
|
36
47
|
|
|
37
48
|
public initWebSocket(url: string) {
|
|
@@ -39,19 +50,21 @@ export class WebSocketClient {
|
|
|
39
50
|
}
|
|
40
51
|
|
|
41
52
|
private setupConnectionMonitor() {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
if (!isApp) {
|
|
54
|
+
window.addEventListener('online', () => {
|
|
55
|
+
console.log('Network is online, attempting to reconnect...');
|
|
56
|
+
this.reconnect();
|
|
57
|
+
});
|
|
46
58
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
59
|
+
window.addEventListener('offline', () => {
|
|
60
|
+
console.log('Network is offline, caching subscriptions...');
|
|
61
|
+
this.cacheSubscriptions();
|
|
62
|
+
});
|
|
51
63
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
64
|
+
window.addEventListener('beforeunload', () => {
|
|
65
|
+
this.disconnect();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
55
68
|
}
|
|
56
69
|
|
|
57
70
|
private getReconnectDelay(): number {
|
|
@@ -64,6 +77,12 @@ export class WebSocketClient {
|
|
|
64
77
|
}
|
|
65
78
|
|
|
66
79
|
private setupWebSocket() {
|
|
80
|
+
|
|
81
|
+
if (isApp && this.wsClient) {
|
|
82
|
+
this.wsClient.setupWebSocket()
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
67
86
|
if (this.ws || !this.url) {
|
|
68
87
|
return;
|
|
69
88
|
}
|
|
@@ -84,9 +103,17 @@ export class WebSocketClient {
|
|
|
84
103
|
next: () => {
|
|
85
104
|
console.log('WebSocket disconnected');
|
|
86
105
|
this.isConnected = false;
|
|
87
|
-
this.
|
|
88
|
-
|
|
89
|
-
|
|
106
|
+
const time = this.getReconnectDelay()
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
this.reconnectAttempts += 1;
|
|
109
|
+
if (this.reconnectAttempts > this.maxReconnectAttempts) {
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
this.cacheSubscriptions();
|
|
113
|
+
this.stopHeartbeat();
|
|
114
|
+
this.reconnect();
|
|
115
|
+
}, time)
|
|
116
|
+
|
|
90
117
|
}
|
|
91
118
|
}
|
|
92
119
|
});
|
|
@@ -112,6 +139,10 @@ export class WebSocketClient {
|
|
|
112
139
|
}
|
|
113
140
|
|
|
114
141
|
private startHeartbeat() {
|
|
142
|
+
if (isApp && this.wsClient) {
|
|
143
|
+
this.wsClient.startHeartbeat()
|
|
144
|
+
return
|
|
145
|
+
}
|
|
115
146
|
this.stopHeartbeat();
|
|
116
147
|
this.heartbeatSubscription = timer(0, 2000).subscribe(() => {
|
|
117
148
|
this.send({ type: 'ping' });
|
|
@@ -119,6 +150,11 @@ export class WebSocketClient {
|
|
|
119
150
|
}
|
|
120
151
|
|
|
121
152
|
private stopHeartbeat() {
|
|
153
|
+
if (isApp && this.wsClient) {
|
|
154
|
+
this.wsClient.stopHeartbeat()
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
|
|
122
158
|
if (this.heartbeatSubscription) {
|
|
123
159
|
this.heartbeatSubscription.unsubscribe();
|
|
124
160
|
this.heartbeatSubscription = null;
|
|
@@ -126,16 +162,22 @@ export class WebSocketClient {
|
|
|
126
162
|
}
|
|
127
163
|
|
|
128
164
|
private handleMessage(message: WebSocketMessage) {
|
|
165
|
+
|
|
166
|
+
if (isApp && this.wsClient) {
|
|
167
|
+
this.wsClient.handleMessage(message)
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
|
|
129
171
|
if (message.type === 'pong') {
|
|
130
172
|
return;
|
|
131
173
|
}
|
|
132
174
|
|
|
133
175
|
if (message.type === 'error') {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
176
|
+
if (this.options.onError) {
|
|
177
|
+
this.options.onError(message)
|
|
178
|
+
} else {
|
|
179
|
+
notification.error({ key: 'error', message: message.message });
|
|
180
|
+
}
|
|
139
181
|
return;
|
|
140
182
|
}
|
|
141
183
|
|
|
@@ -151,6 +193,11 @@ export class WebSocketClient {
|
|
|
151
193
|
}
|
|
152
194
|
|
|
153
195
|
private processTempQueue() {
|
|
196
|
+
if (isApp && this.wsClient) {
|
|
197
|
+
this.wsClient.processTempQueue()
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
|
|
154
201
|
while (this.tempQueue.length > 0) {
|
|
155
202
|
const message = this.tempQueue.shift();
|
|
156
203
|
if (message) {
|
|
@@ -160,11 +207,19 @@ export class WebSocketClient {
|
|
|
160
207
|
}
|
|
161
208
|
|
|
162
209
|
private cacheSubscriptions() {
|
|
210
|
+
if (isApp && this.wsClient) {
|
|
211
|
+
this.wsClient.cacheSubscriptions()
|
|
212
|
+
return
|
|
213
|
+
}
|
|
163
214
|
this.pendingSubscriptions = new Map(this.subscriptions);
|
|
164
215
|
this.subscriptions.clear();
|
|
165
216
|
}
|
|
166
217
|
|
|
167
218
|
private restoreSubscriptions() {
|
|
219
|
+
if (isApp && this.wsClient) {
|
|
220
|
+
this.wsClient.restoreSubscriptions()
|
|
221
|
+
return
|
|
222
|
+
}
|
|
168
223
|
this.pendingSubscriptions.forEach((subject, id) => {
|
|
169
224
|
this.subscriptions.set(id, subject);
|
|
170
225
|
});
|
|
@@ -172,6 +227,10 @@ export class WebSocketClient {
|
|
|
172
227
|
}
|
|
173
228
|
|
|
174
229
|
private reconnect() {
|
|
230
|
+
if (isApp && this.wsClient) {
|
|
231
|
+
this.wsClient.reconnect()
|
|
232
|
+
return
|
|
233
|
+
}
|
|
175
234
|
if (!this.isConnected && navigator.onLine) {
|
|
176
235
|
this.ws = null;
|
|
177
236
|
this.setupWebSocket();
|
|
@@ -179,10 +238,18 @@ export class WebSocketClient {
|
|
|
179
238
|
}
|
|
180
239
|
|
|
181
240
|
public connect() {
|
|
241
|
+
if (isApp && this.wsClient) {
|
|
242
|
+
this.wsClient.connect()
|
|
243
|
+
return
|
|
244
|
+
}
|
|
182
245
|
this.setupWebSocket();
|
|
183
246
|
}
|
|
184
247
|
|
|
185
248
|
public disconnect() {
|
|
249
|
+
if (isApp && this.wsClient) {
|
|
250
|
+
this.wsClient.disconnect()
|
|
251
|
+
return
|
|
252
|
+
}
|
|
186
253
|
if (this.ws) {
|
|
187
254
|
this.ws.complete();
|
|
188
255
|
this.ws = null;
|
|
@@ -194,6 +261,10 @@ export class WebSocketClient {
|
|
|
194
261
|
}
|
|
195
262
|
|
|
196
263
|
public send(message: WebSocketMessage) {
|
|
264
|
+
if (isApp && this.wsClient) {
|
|
265
|
+
this.wsClient.send(message)
|
|
266
|
+
return
|
|
267
|
+
}
|
|
197
268
|
if (this.ws && this.isConnected) {
|
|
198
269
|
this.ws.next(message);
|
|
199
270
|
} else {
|
|
@@ -202,6 +273,15 @@ export class WebSocketClient {
|
|
|
202
273
|
}
|
|
203
274
|
|
|
204
275
|
public getWebSocket(id: string, topic: string, parameter: Record<string, any> = {}): Observable<WebSocketMessage> {
|
|
276
|
+
console.log('getWebSocket', this.wsClient, id)
|
|
277
|
+
if (isApp && this.wsClient) {
|
|
278
|
+
return this.wsClient.getWebSocket(
|
|
279
|
+
id,
|
|
280
|
+
topic,
|
|
281
|
+
parameter
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
205
285
|
const subject = new Subject<WebSocketMessage>();
|
|
206
286
|
this.subscriptions.set(id, subject);
|
|
207
287
|
|
|
@@ -235,9 +315,9 @@ export class WebSocketClient {
|
|
|
235
315
|
* .subscribe(
|
|
236
316
|
* message => console.log('Received:', message)
|
|
237
317
|
* );
|
|
238
|
-
*
|
|
318
|
+
*
|
|
239
319
|
* // 清理
|
|
240
320
|
* subscription.unsubscribe();
|
|
241
|
-
*
|
|
321
|
+
*
|
|
242
322
|
*/
|
|
243
323
|
export const wsClient = new WebSocketClient();
|