@@ -3,7 +3,6 @@ var _ = require('underscore');
33var  moment  =  require ( "moment" ) ; 
44var  config  =  require ( "./config" ) ; 
55var  allClients  =  [ ] ; 
6- var  userList  =  [ ] ; 
76io . on ( 'connection' ,  function ( socket ) { 
87
98	//push new socket info to allClients 
@@ -16,46 +15,29 @@ io.on('connection', function(socket){
1615	socket . on ( 'join' ,  function ( room ,  user ,  oldNick ) { 
1716    var  time  =  moment ( ) . format ( 'MMMM Do YYYY, h:mm:ss a' ) ; 
1817  	var  userExists  =  false ; 
19-   	var  userLog  =  false ; 
20-     var  x  =  0 ; 
18+     var  users  =  [ ] ; 
2119
2220  	//iterate over allClients to check userName for duplicates 
2321  	allClients . forEach ( function ( client ) { 
24-   		if ( client . userName  ==  user  &&   client . conn . remoteAddress   !=   allClients [ i ] . conn . remoteAddress   ||  user  ==  'undefined' ) { 
22+   		if ( client . userName  ==  user  ||  user  ==  'undefined' ) { 
2523  			userExists  =  true ; 
2624  		} 
2725  	} ) 
2826  	if ( userExists  ==  false  &&  user  !=  null ) { 
2927  		//send current user to room once no conflicts with users 
30-       allClients . forEach ( function ( client ) { 
31-         if ( user  ==  client . userName  &&  room  ==  client . room ) { 
32-           x ++ ; 
33-         } 
34-       } ) 
35-       if ( x  <=  0 ) { 
3628			socket . broadcast . emit ( 'user.join' ,  room ,  user ,  time ,  oldNick ) ; 
37-       } 
29+       
3830			allClients [ i ] . userName  =  user ; 
3931			allClients [ i ] . room  =  room ; 
4032
41- 			if ( typeof  userList [ room ]  ==  'undefined' ) { 
42- 				userList [ room ]  =  [ ] ; 
43- 			} 
44-       if ( oldNick ) { 
45-         for ( list  in  userList ) { 
46-           var  index  =  userList [ list ] . indexOf ( oldNick ) ; 
47-           if ( index  !=  - 1 ) { 
48-             userList [ list ] . splice ( index ,  1 ) ; 
49-             userList [ list ] . push ( user ) ; 
50-           } 
33+       allClients . forEach ( function ( client ) { 
34+         if ( room  ==  client . room ) { 
35+           users . push ( client . userName ) 
5136        } 
52-       } 
53- 			if ( ! _ . contains ( userList [ room ] ,  user ) ) { 
54- 				userList [ room ] . push ( user ) ; 
55- 			} 
37+       } ) 
5638
5739			//send list of users in current room to current client 
58-   		io . sockets . connected [ allClients [ i ] . id ] . emit ( 'getUserList' ,  userList [ room ] . join ( "," ) ,  time ) ; 
40+   		io . sockets . connected [ allClients [ i ] . id ] . emit ( 'getUserList' ,  users . join ( "," ) ,  time ) ; 
5941
6042		}  else  if  ( userExists  ==  true )  { 
6143			//send prompt to current user/socket connected 
@@ -68,6 +50,7 @@ io.on('connection', function(socket){
6850    var  time  =  moment ( ) . format ( 'MMMM Do YYYY, h:mm:ss a' ) ; 
6951    var  cmd  =  parseArgs ( message ) [ 0 ] ; 
7052    var  usr  =  parseArgs ( message ) [ 1 ] ; 
53+     var  users  =  [ ] ; 
7154
7255    switch ( cmd ) { 
7356      //prompt user to reset their nickname 
@@ -80,7 +63,13 @@ io.on('connection', function(socket){
8063      break ; 
8164      //list all users in current room 
8265      case  "/list" :
83-         io . sockets . connected [ allClients [ i ] . id ] . emit ( 'message' ,  room ,  '*' ,  'Current users in room are '  +  userList [ room ] ,  time ) ; 
66+         allClients . forEach ( function ( client ) { 
67+           if ( client . room  ==  room ) { 
68+             users . push ( client . userName ) 
69+           } 
70+         } ) 
71+ 
72+         io . sockets . connected [ allClients [ i ] . id ] . emit ( 'message' ,  room ,  '*' ,  'Current users in room are '  +  users . join ( "," ) ,  time ) ; 
8473      break ; 
8574      //ability to private message user with /pm <@username> <message> 
8675      case  "/pm" :
@@ -113,33 +102,11 @@ io.on('connection', function(socket){
113102  socket . on ( 'disconnect' ,  function ( )  { 
114103    var  time  =  moment ( ) . format ( 'MMMM Do YYYY, h:mm:ss a' ) ; 
115104    var  s  =  allClients . indexOf ( socket ) ; 
116-     var  x  =  0 ; 
117105    var  user  =  allClients [ s ] . userName ; 
118-     var  room  =  allClients [ s ] . room ; 
119-     
106+     var  room  =  allClients [ s ] . room ;  
120107    //check if room exists and has current disconnect name. If so remove from array 
121-     if ( typeof  userList [ room ]  !=  "undefined" ) { 
122-       for ( list  in  userList ) { 
123-         var  index  =  userList [ list ] . indexOf ( user ) ; 
124-         allClients . forEach ( function ( client ) { 
125-           if ( user  ==  client . userName  &&  room  ==  client . room ) { 
126-             x ++ ; 
127-           } 
128-         } ) 
129-         if ( x  <=  1 ) { 
130-           if ( index  !=  - 1 ) { 
131-             userList [ list ] . splice ( index ,  1 ) ; 
132- 
133-           } 
134-           //send disconnect message to chat room 
135-           io . emit ( 'disconnect' ,  room ,  user ,  time ) ; 
136-         } 
137-       } 
138-     } 
108+     io . emit ( 'disconnect' ,  room ,  user ,  time ) ; 
139109    delete  allClients [ s ] ; 
140-     
141-     
142- 
143110  } ) ; 
144111
145112} ) ; 
0 commit comments