@@ -21,6 +21,7 @@ interface IConstructorParams {
21
21
loggerLevel ?: LoggerLevel ;
22
22
httpInstance ?: HttpInstance ;
23
23
autoReconnect ?: boolean ;
24
+ agent ?: any ;
24
25
}
25
26
26
27
export class WSClient {
@@ -46,21 +47,23 @@ export class WSClient {
46
47
}
47
48
48
49
constructor ( params : IConstructorParams ) {
49
- const {
50
- appId,
51
- appSecret,
52
- domain = Domain . Feishu ,
50
+ const {
51
+ appId,
52
+ appSecret,
53
+ agent,
54
+ domain = Domain . Feishu ,
53
55
httpInstance = defaultHttpInstance ,
54
56
loggerLevel = LoggerLevel . info ,
55
57
logger = defaultLogger ,
56
58
autoReconnect = true
57
59
} = params ;
58
60
59
61
this . logger = new LoggerProxy ( loggerLevel , logger ) ;
60
-
62
+
61
63
assert ( ! appId , ( ) => this . logger . error ( 'appId is needed' ) ) ;
62
64
assert ( ! appSecret , ( ) => this . logger . error ( 'appSecret is needed' ) ) ;
63
-
65
+
66
+ this . agent = agent ;
64
67
this . dataCache = new DataCache ( { logger : this . logger } ) ;
65
68
this . httpInstance = httpInstance ;
66
69
this . wsConfig . updateClient ( {
@@ -72,10 +75,10 @@ export class WSClient {
72
75
this . wsConfig . updateWs ( {
73
76
autoReconnect
74
77
} )
75
- }
78
+ }
76
79
77
80
private async pullConnectConfig ( ) {
78
- const {
81
+ const {
79
82
appId,
80
83
appSecret
81
84
} = this . wsConfig . getClient ( ) ;
@@ -116,7 +119,7 @@ export class WSClient {
116
119
117
120
this . wsConfig . updateWs ( {
118
121
connectUrl : URL ,
119
-
122
+
120
123
deviceId : device_id as string ,
121
124
serviceId : service_id as string ,
122
125
@@ -141,7 +144,8 @@ export class WSClient {
141
144
let wsInstance ;
142
145
143
146
try {
144
- wsInstance = new WebSocket ( connectUrl ) ;
147
+ const { agent } = this ;
148
+ wsInstance = new WebSocket ( connectUrl , { agent } ) ;
145
149
} catch ( e ) {
146
150
this . logger . error ( '[ws]' , 'new WebSocket error' ) ;
147
151
}
@@ -214,17 +218,17 @@ export class WSClient {
214
218
}
215
219
216
220
const { autoReconnect, reconnectNonce, reconnectCount, reconnectInterval } = this . wsConfig . getWS ( ) ;
217
-
221
+
218
222
if ( ! autoReconnect ) {
219
223
return ;
220
224
}
221
225
222
226
this . logger . info ( '[ws]' , 'reconnect' ) ;
223
-
227
+
224
228
if ( wsInstance ) {
225
229
wsInstance ?. terminate ( ) ;
226
230
}
227
-
231
+
228
232
this . wsConfig . setWSInstance ( null ) ;
229
233
230
234
const reconnectNonceTime = reconnectNonce ? reconnectNonce * Math . random ( ) : 0
@@ -256,7 +260,7 @@ export class WSClient {
256
260
}
257
261
258
262
private pingLoop ( ) {
259
- const {
263
+ const {
260
264
serviceId,
261
265
pingInterval
262
266
} = this . wsConfig . getWS ( ) ;
@@ -310,7 +314,7 @@ export class WSClient {
310
314
private async handleControlData ( data : pbbp2 . Frame ) {
311
315
const type = data . headers . find ( item => item . key === HeaderKey . type ) ?. value ;
312
316
const payload = data . payload ;
313
-
317
+
314
318
if ( type === MessageType . ping ) {
315
319
return ;
316
320
}
0 commit comments