@@ -35,8 +35,8 @@ Listener = module.exports = function(server, options){
3535 require ( 'sys' ) . log ( message ) ;
3636 }
3737 } , options ) ;
38- this . clients = [ ] ;
39- this . clientsIndex = { } ;
38+
39+ this . clients = { } ;
4040
4141 var listeners = this . server . listeners ( 'request' ) ;
4242 this . server . removeAllListeners ( 'request' ) ;
@@ -65,9 +65,9 @@ sys.inherits(Listener, process.EventEmitter);
6565for ( var i in options ) Listener . prototype [ i ] = options [ i ] ;
6666
6767Listener . prototype . broadcast = function ( message , except ) {
68- for ( var i = 0 , l = this . clients . length ; i < l ; i ++ ) {
69- if ( this . clients [ i ] && ( ! except || [ ] . concat ( except ) . indexOf ( this . clients [ i ] . sessionId ) == - 1 ) ) {
70- this . clients [ i ] . send ( message ) ;
68+ for ( var i = 0 , k = Object . keys ( this . clients ) , l = k . length ; i < l ; i ++ ) {
69+ if ( this . clients [ k [ i ] ] && ( ! except || [ ] . concat ( except ) . indexOf ( this . clients [ k [ i ] ] . sessionId ) == - 1 ) ) {
70+ this . clients [ k [ i ] ] . send ( message ) ;
7171 }
7272 }
7373 return this ;
@@ -79,7 +79,7 @@ Listener.prototype.check = function(req, res, httpUpgrade, head){
7979 parts = path . substr ( 1 ) . split ( '/' ) ;
8080 if ( ! ( parts [ 1 ] in transports ) ) return false ;
8181 if ( parts [ 2 ] ) {
82- cn = this . _lookupClient ( parts [ 2 ] ) ;
82+ cn = this . clients [ parts [ 2 ] ] ;
8383 if ( cn ) {
8484 cn . _onConnect ( req , res ) ;
8585 } else {
@@ -94,17 +94,11 @@ Listener.prototype.check = function(req, res, httpUpgrade, head){
9494 return false ;
9595} ;
9696
97- Listener . prototype . _lookupClient = function ( sid ) {
98- return this . clientsIndex [ sid ] ;
99- } ;
100-
10197Listener . prototype . _onClientConnect = function ( client ) {
10298 if ( ! ( client instanceof Client ) || ! client . sessionId ) {
10399 return this . options . log ( 'Invalid client' ) ;
104100 }
105- client . i = this . clients . length ;
106- this . clients . push ( client ) ;
107- this . clientsIndex [ client . sessionId ] = client ;
101+ this . clients [ client . sessionId ] = client ;
108102 this . options . log ( 'Client ' + client . sessionId + ' connected' ) ;
109103 this . emit ( 'clientConnect' , client ) ;
110104 this . emit ( 'connection' , client ) ;
@@ -115,8 +109,7 @@ Listener.prototype._onClientMessage = function(data, client){
115109} ;
116110
117111Listener . prototype . _onClientDisconnect = function ( client ) {
118- this . clientsIndex [ client . sessionId ] = null ;
119- this . clients [ client . i ] = null ;
112+ delete this . clients [ client . sessionId ] ;
120113 this . options . log ( 'Client ' + client . sessionId + ' disconnected' ) ;
121114 this . emit ( 'clientDisconnect' , client ) ;
122115} ;
0 commit comments