1
1
import uid2 = require( "uid2" ) ;
2
2
import msgpack = require( "notepack.io" ) ;
3
3
import { Adapter , BroadcastOptions , Room } from "socket.io-adapter" ;
4
+ import { parseNumSubResponse , sumValues } from "./util" ;
4
5
5
6
const debug = require ( "debug" ) ( "socket.io-redis" ) ;
6
7
@@ -688,7 +689,7 @@ export class RedisAdapter extends Adapter {
688
689
*/
689
690
public async allRooms ( ) : Promise < Set < Room > > {
690
691
const localRooms = new Set ( this . rooms . keys ( ) ) ;
691
- const numSub = await this . getNumSub ( ) ;
692
+ const numSub = await this . serverCount ( ) ;
692
693
debug ( 'waiting for %d responses to "allRooms" request' , numSub ) ;
693
694
694
695
if ( numSub <= 1 ) {
@@ -732,7 +733,7 @@ export class RedisAdapter extends Adapter {
732
733
return localSockets ;
733
734
}
734
735
735
- const numSub = await this . getNumSub ( ) ;
736
+ const numSub = await this . serverCount ( ) ;
736
737
debug ( 'waiting for %d responses to "fetchSockets" request' , numSub ) ;
737
738
738
739
if ( numSub <= 1 ) {
@@ -849,7 +850,7 @@ export class RedisAdapter extends Adapter {
849
850
850
851
private async serverSideEmitWithAck ( packet : any [ ] ) {
851
852
const ack = packet . pop ( ) ;
852
- const numSub = ( await this . getNumSub ( ) ) - 1 ; // ignore self
853
+ const numSub = ( await this . serverCount ( ) ) - 1 ; // ignore self
853
854
854
855
debug ( 'waiting for %d responses to "serverSideEmit" request' , numSub ) ;
855
856
@@ -889,13 +890,7 @@ export class RedisAdapter extends Adapter {
889
890
this . pubClient . publish ( this . requestChannel , request ) ;
890
891
}
891
892
892
- /**
893
- * Get the number of subscribers of the request channel
894
- *
895
- * @private
896
- */
897
-
898
- private getNumSub ( ) : Promise < number > {
893
+ override serverCount ( ) : Promise < number > {
899
894
if (
900
895
this . pubClient . constructor . name === "Cluster" ||
901
896
this . pubClient . isCluster
@@ -904,39 +899,27 @@ export class RedisAdapter extends Adapter {
904
899
const nodes = this . pubClient . nodes ( ) ;
905
900
return Promise . all (
906
901
nodes . map ( ( node ) =>
907
- node . send_command ( "pubsub" , [ "numsub" , this . requestChannel ] )
902
+ node
903
+ . send_command ( "pubsub" , [ "numsub" , this . requestChannel ] )
904
+ . then ( parseNumSubResponse )
908
905
)
909
- ) . then ( ( values ) => {
910
- let numSub = 0 ;
911
- values . forEach ( ( value ) => {
912
- numSub += parseInt ( value [ 1 ] , 10 ) ;
913
- } ) ;
914
- return numSub ;
915
- } ) ;
906
+ ) . then ( sumValues ) ;
916
907
} else if ( typeof this . pubClient . pSubscribe === "function" ) {
917
908
// node-redis client
918
909
const isCluster = Array . isArray ( this . pubClient . masters ) ;
919
910
if ( isCluster ) {
920
911
const nodes = this . pubClient . masters ;
921
912
return Promise . all (
922
913
nodes . map ( ( node ) => {
923
- return node . client . sendCommand ( [
924
- "pubsub" ,
925
- "numsub" ,
926
- this . requestChannel ,
927
- ] ) ;
914
+ return node . client
915
+ . sendCommand ( [ "pubsub" , "numsub" , this . requestChannel ] )
916
+ . then ( parseNumSubResponse ) ;
928
917
} )
929
- ) . then ( ( values ) => {
930
- let numSub = 0 ;
931
- values . map ( ( value ) => {
932
- numSub += parseInt ( value [ 1 ] , 10 ) ;
933
- } ) ;
934
- return numSub ;
935
- } ) ;
918
+ ) . then ( sumValues ) ;
936
919
} else {
937
920
return this . pubClient
938
921
. sendCommand ( [ "pubsub" , "numsub" , this . requestChannel ] )
939
- . then ( ( res ) => parseInt ( res [ 1 ] , 10 ) ) ;
922
+ . then ( parseNumSubResponse ) ;
940
923
}
941
924
} else {
942
925
// ioredis or node-redis v3 client
@@ -946,17 +929,13 @@ export class RedisAdapter extends Adapter {
946
929
[ "numsub" , this . requestChannel ] ,
947
930
( err , numSub ) => {
948
931
if ( err ) return reject ( err ) ;
949
- resolve ( parseInt ( numSub [ 1 ] , 10 ) ) ;
932
+ resolve ( parseNumSubResponse ( numSub ) ) ;
950
933
}
951
934
) ;
952
935
} ) ;
953
936
}
954
937
}
955
938
956
- serverCount ( ) : Promise < number > {
957
- return this . getNumSub ( ) ;
958
- }
959
-
960
939
close ( ) : Promise < void > | void {
961
940
const isRedisV4 = typeof this . pubClient . pSubscribe === "function" ;
962
941
if ( isRedisV4 ) {
0 commit comments