@@ -8,7 +8,6 @@ var redis = require('redis').createClient;
8
8
var msgpack = require ( 'msgpack-lite' ) ;
9
9
var Adapter = require ( 'socket.io-adapter' ) ;
10
10
var debug = require ( 'debug' ) ( 'socket.io-redis' ) ;
11
- var async = require ( 'async' ) ;
12
11
13
12
/**
14
13
* Module exports.
@@ -50,11 +49,8 @@ function adapter(uri, opts) {
50
49
// opts
51
50
var pub = opts . pubClient ;
52
51
var sub = opts . subClient ;
53
-
54
52
var prefix = opts . key || 'socket.io' ;
55
- var subEvent = opts . subEvent || 'messageBuffer' ;
56
53
var requestsTimeout = opts . requestsTimeout || 1000 ;
57
- var withChannelMultiplexing = false !== opts . withChannelMultiplexing ;
58
54
59
55
// init clients if needed
60
56
function createClient ( ) {
@@ -85,7 +81,6 @@ function adapter(uri, opts) {
85
81
this . uid = uid ;
86
82
this . prefix = prefix ;
87
83
this . requestsTimeout = requestsTimeout ;
88
- this . withChannelMultiplexing = withChannelMultiplexing ;
89
84
90
85
this . channel = prefix + '#' + nsp . name + '#' ;
91
86
this . requestChannel = prefix + '-request#' + this . nsp . name + '#' ;
@@ -107,11 +102,17 @@ function adapter(uri, opts) {
107
102
108
103
var self = this ;
109
104
110
- sub . subscribe ( [ this . channel , this . requestChannel , this . responseChannel ] , function ( err ) {
105
+ sub . psubscribe ( this . channel + '*' , function ( err ) {
106
+ if ( err ) self . emit ( 'error' , err ) ;
107
+ } ) ;
108
+
109
+ sub . on ( 'pmessageBuffer' , this . onmessage . bind ( this ) ) ;
110
+
111
+ sub . subscribe ( [ this . requestChannel , this . responseChannel ] , function ( err ) {
111
112
if ( err ) self . emit ( 'error' , err ) ;
112
113
} ) ;
113
114
114
- sub . on ( subEvent , this . onmessage . bind ( this ) ) ;
115
+ sub . on ( 'messageBuffer' , this . onrequest . bind ( this ) ) ;
115
116
116
117
function onError ( err ) {
117
118
self . emit ( 'error' , err ) ;
@@ -132,21 +133,22 @@ function adapter(uri, opts) {
132
133
* @api private
133
134
*/
134
135
135
- Redis . prototype . onmessage = function ( channel , msg ) {
136
+ Redis . prototype . onmessage = function ( pattern , channel , msg ) {
136
137
channel = channel . toString ( ) ;
137
138
138
- if ( this . channelMatches ( channel , this . requestChannel ) ) {
139
- return this . onrequest ( channel , msg ) ;
140
- } else if ( this . channelMatches ( channel , this . responseChannel ) ) {
141
- return this . onresponse ( channel , msg ) ;
142
- } else if ( ! this . channelMatches ( channel , this . channel ) ) {
139
+ if ( ! this . channelMatches ( channel , this . channel ) ) {
143
140
return debug ( 'ignore different channel' ) ;
144
141
}
145
142
143
+ var room = channel . substring ( this . channel . length ) ;
144
+ if ( room !== '' && ! this . rooms . hasOwnProperty ( room ) ) {
145
+ return debug ( 'ignore unknown room %s' , room ) ;
146
+ }
147
+
146
148
var args = msgpack . decode ( msg ) ;
147
149
var packet ;
148
150
149
- if ( uid == args . shift ( ) ) return debug ( 'ignore same uid' ) ;
151
+ if ( uid === args . shift ( ) ) return debug ( 'ignore same uid' ) ;
150
152
151
153
packet = args [ 0 ] ;
152
154
@@ -170,6 +172,14 @@ function adapter(uri, opts) {
170
172
*/
171
173
172
174
Redis . prototype . onrequest = function ( channel , msg ) {
175
+ channel = channel . toString ( ) ;
176
+
177
+ if ( this . channelMatches ( channel , this . responseChannel ) ) {
178
+ return this . onresponse ( channel , msg ) ;
179
+ } else if ( ! this . channelMatches ( channel , this . requestChannel ) ) {
180
+ return debug ( 'ignore different channel' ) ;
181
+ }
182
+
173
183
var self = this ;
174
184
var request ;
175
185
@@ -394,116 +404,15 @@ function adapter(uri, opts) {
394
404
packet . nsp = this . nsp . name ;
395
405
if ( ! ( remote || ( opts && opts . flags && opts . flags . local ) ) ) {
396
406
var msg = msgpack . encode ( [ uid , packet , opts ] ) ;
397
- if ( this . withChannelMultiplexing && opts . rooms && opts . rooms . length === 1 ) {
398
- pub . publish ( this . channel + opts . rooms [ 0 ] + '#' , msg ) ;
407
+ if ( opts . rooms && opts . rooms . length === 1 ) {
408
+ pub . publish ( this . channel + opts . rooms [ 0 ] , msg ) ;
399
409
} else {
400
410
pub . publish ( this . channel , msg ) ;
401
411
}
402
412
}
403
413
Adapter . prototype . broadcast . call ( this , packet , opts ) ;
404
414
} ;
405
415
406
- /**
407
- * Subscribe client to room messages.
408
- *
409
- * @param {String } client id
410
- * @param {String } room
411
- * @param {Function } callback (optional)
412
- * @api public
413
- */
414
-
415
- Redis . prototype . add = function ( id , room , fn ) {
416
- debug ( 'adding %s to %s ' , id , room ) ;
417
- var self = this ;
418
- // subscribe only once per room
419
- var alreadyHasRoom = this . rooms . hasOwnProperty ( room ) ;
420
- Adapter . prototype . add . call ( this , id , room ) ;
421
-
422
- if ( ! this . withChannelMultiplexing || alreadyHasRoom ) {
423
- if ( fn ) fn ( null ) ;
424
- return ;
425
- }
426
-
427
- var channel = this . channel + room + '#' ;
428
-
429
- function onSubscribe ( err ) {
430
- if ( err ) {
431
- self . emit ( 'error' , err ) ;
432
- if ( fn ) fn ( err ) ;
433
- return ;
434
- }
435
- if ( fn ) fn ( null ) ;
436
- }
437
-
438
- sub . subscribe ( channel , onSubscribe ) ;
439
- } ;
440
-
441
- /**
442
- * Unsubscribe client from room messages.
443
- *
444
- * @param {String } session id
445
- * @param {String } room id
446
- * @param {Function } callback (optional)
447
- * @api public
448
- */
449
-
450
- Redis . prototype . del = function ( id , room , fn ) {
451
- debug ( 'removing %s from %s' , id , room ) ;
452
-
453
- var self = this ;
454
- var hasRoom = this . rooms . hasOwnProperty ( room ) ;
455
- Adapter . prototype . del . call ( this , id , room ) ;
456
-
457
- if ( this . withChannelMultiplexing && hasRoom && ! this . rooms [ room ] ) {
458
- var channel = this . channel + room + '#' ;
459
-
460
- function onUnsubscribe ( err ) {
461
- if ( err ) {
462
- self . emit ( 'error' , err ) ;
463
- if ( fn ) fn ( err ) ;
464
- return ;
465
- }
466
- if ( fn ) fn ( null ) ;
467
- }
468
-
469
- sub . unsubscribe ( channel , onUnsubscribe ) ;
470
- } else {
471
- if ( fn ) process . nextTick ( fn . bind ( null , null ) ) ;
472
- }
473
- } ;
474
-
475
- /**
476
- * Unsubscribe client completely.
477
- *
478
- * @param {String } client id
479
- * @param {Function } callback (optional)
480
- * @api public
481
- */
482
-
483
- Redis . prototype . delAll = function ( id , fn ) {
484
- debug ( 'removing %s from all rooms' , id ) ;
485
-
486
- var self = this ;
487
- var rooms = this . sids [ id ] ;
488
-
489
- if ( ! rooms ) {
490
- if ( fn ) process . nextTick ( fn . bind ( null , null ) ) ;
491
- return ;
492
- }
493
-
494
- async . each ( Object . keys ( rooms ) , function ( room , next ) {
495
- self . del ( id , room , next ) ;
496
- } , function ( err ) {
497
- if ( err ) {
498
- self . emit ( 'error' , err ) ;
499
- if ( fn ) fn ( err ) ;
500
- return ;
501
- }
502
- delete self . sids [ id ] ;
503
- if ( fn ) fn ( null ) ;
504
- } ) ;
505
- } ;
506
-
507
416
/**
508
417
* Gets a list of clients by sid.
509
418
*
@@ -531,6 +440,7 @@ function adapter(uri, opts) {
531
440
}
532
441
533
442
numsub = parseInt ( numsub [ 1 ] , 10 ) ;
443
+ debug ( 'waiting for %d responses to "clients" request' , numsub ) ;
534
444
535
445
var request = JSON . stringify ( {
536
446
requestid : requestid ,
@@ -619,6 +529,7 @@ function adapter(uri, opts) {
619
529
}
620
530
621
531
numsub = parseInt ( numsub [ 1 ] , 10 ) ;
532
+ debug ( 'waiting for %d responses to "allRooms" request' , numsub ) ;
622
533
623
534
var request = JSON . stringify ( {
624
535
requestid : requestid ,
@@ -794,6 +705,7 @@ function adapter(uri, opts) {
794
705
}
795
706
796
707
numsub = parseInt ( numsub [ 1 ] , 10 ) ;
708
+ debug ( 'waiting for %d responses to "customRequest" request' , numsub ) ;
797
709
798
710
var request = JSON . stringify ( {
799
711
requestid : requestid ,
0 commit comments