@@ -9,10 +9,11 @@ import { matchesQuery, queryHash } from './QueryTools';
9
9
import { ParsePubSub } from './ParsePubSub' ;
10
10
import { SessionTokenCache } from './SessionTokenCache' ;
11
11
import _ from 'lodash' ;
12
+ import uuid from 'uuid' ;
13
+ import { runLiveQueryEventHandlers } from '../triggers' ;
12
14
13
15
class ParseLiveQueryServer {
14
- clientId : number ;
15
- clients : Object ;
16
+ clients : Map ;
16
17
// className -> (queryHash -> subscription)
17
18
subscriptions : Object ;
18
19
parseWebSocketServer : Object ;
@@ -21,7 +22,6 @@ class ParseLiveQueryServer {
21
22
subscriber : Object ;
22
23
23
24
constructor ( server : any , config : any ) {
24
- this . clientId = 0 ;
25
25
this . clients = new Map ( ) ;
26
26
this . subscriptions = new Map ( ) ;
27
27
@@ -269,10 +269,16 @@ class ParseLiveQueryServer {
269
269
} ) ;
270
270
271
271
parseWebsocket . on ( 'disconnect' , ( ) => {
272
- logger . info ( ' Client disconnect: %d' , parseWebsocket . clientId ) ;
272
+ logger . info ( ` Client disconnect: ${ parseWebsocket . clientId } ` ) ;
273
273
const clientId = parseWebsocket . clientId ;
274
274
if ( ! this . clients . has ( clientId ) ) {
275
- logger . error ( 'Can not find client %d on disconnect' , clientId ) ;
275
+ runLiveQueryEventHandlers ( {
276
+ event : 'ws_disconnect_error' ,
277
+ clients : this . clients . size ,
278
+ subscriptions : this . subscriptions . size ,
279
+ error : `Unable to find client ${ clientId } `
280
+ } ) ;
281
+ logger . error ( `Can not find client ${ clientId } on disconnect` ) ;
276
282
return ;
277
283
}
278
284
@@ -298,6 +304,17 @@ class ParseLiveQueryServer {
298
304
299
305
logger . verbose ( 'Current clients %d' , this . clients . size ) ;
300
306
logger . verbose ( 'Current subscriptions %d' , this . subscriptions . size ) ;
307
+ runLiveQueryEventHandlers ( {
308
+ event : 'ws_disconnect' ,
309
+ clients : this . clients . size ,
310
+ subscriptions : this . subscriptions . size
311
+ } ) ;
312
+ } ) ;
313
+
314
+ runLiveQueryEventHandlers ( {
315
+ event : 'ws_connect' ,
316
+ clients : this . clients . size ,
317
+ subscriptions : this . subscriptions . size
301
318
} ) ;
302
319
}
303
320
@@ -404,12 +421,17 @@ class ParseLiveQueryServer {
404
421
return ;
405
422
}
406
423
const hasMasterKey = this . _hasMasterKey ( request , this . keyPairs ) ;
407
- const client = new Client ( this . clientId , parseWebsocket , hasMasterKey ) ;
408
- parseWebsocket . clientId = this . clientId ;
409
- this . clientId += 1 ;
424
+ const clientId = uuid ( ) ;
425
+ const client = new Client ( clientId , parseWebsocket , hasMasterKey ) ;
426
+ parseWebsocket . clientId = clientId ;
410
427
this . clients . set ( parseWebsocket . clientId , client ) ;
411
- logger . info ( ' Create new client: %d' , parseWebsocket . clientId ) ;
428
+ logger . info ( ` Create new client: ${ parseWebsocket . clientId } ` ) ;
412
429
client . pushConnect ( ) ;
430
+ runLiveQueryEventHandlers ( {
431
+ event : 'connect' ,
432
+ clients : this . clients . size ,
433
+ subscriptions : this . subscriptions . size
434
+ } ) ;
413
435
}
414
436
415
437
_hasMasterKey ( request : any , validKeyPairs : any ) : boolean {
@@ -481,8 +503,13 @@ class ParseLiveQueryServer {
481
503
482
504
client . pushSubscribe ( request . requestId ) ;
483
505
484
- logger . verbose ( ' Create client %d new subscription: %d' , parseWebsocket . clientId , request . requestId ) ;
506
+ logger . verbose ( ` Create client ${ parseWebsocket . clientId } new subscription: ${ request . requestId } ` ) ;
485
507
logger . verbose ( 'Current client number: %d' , this . clients . size ) ;
508
+ runLiveQueryEventHandlers ( {
509
+ event : 'subscribe' ,
510
+ clients : this . clients . size ,
511
+ subscriptions : this . subscriptions . size
512
+ } ) ;
486
513
}
487
514
488
515
_handleUpdateSubscription ( parseWebsocket : any , request : any ) : any {
@@ -529,14 +556,19 @@ class ParseLiveQueryServer {
529
556
if ( classSubscriptions . size === 0 ) {
530
557
this . subscriptions . delete ( className ) ;
531
558
}
559
+ runLiveQueryEventHandlers ( {
560
+ event : 'unsubscribe' ,
561
+ clients : this . clients . size ,
562
+ subscriptions : this . subscriptions . size
563
+ } ) ;
532
564
533
565
if ( ! notifyClient ) {
534
566
return ;
535
567
}
536
568
537
569
client . pushUnsubscribe ( request . requestId ) ;
538
570
539
- logger . verbose ( ' Delete client: %d | subscription: %d' , parseWebsocket . clientId , request . requestId ) ;
571
+ logger . verbose ( ` Delete client: ${ parseWebsocket . clientId } | subscription: ${ request . requestId } ` ) ;
540
572
}
541
573
}
542
574
0 commit comments