Guest User

Untitled

a guest
Jan 4th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. startGameServer();
  2.  
  3. function startGameServer() {
  4.  
  5.   handleLogins();
  6.  
  7.  
  8.   clientsBuildLoginScreen();
  9.   handleClientsLogin();
  10.  
  11.   function clientsBuildLoginScreen() {
  12.     io.emit('build login screen');
  13.   }
  14.  
  15.   function handleClientsLogin() {
  16.     app.on('all login screens built' function() {
  17.       readyForLoginCredentials = true;
  18.       io.emit('ready for login credentials');
  19.     });
  20.   }
  21.  
  22.   // These are variables that multiple sockets need access to
  23.   var readyForLoginCredentials = false;
  24.   var numberOfSocketsWithBuiltLoginScreens = 0;
  25.   function handleClientConnections() {
  26.     io.on('connection', function(socket) {
  27.    
  28.       socket.on('login screen built', socketLoginScreenBuilt);
  29.  
  30.       function socketLoginScreenBuilt() {
  31.         // See how I'm using this conditional to prevent sockets from triggering the important event logic after they've already triggered it once (since I set socket.loginBuilt to true in the logic
  32.         if (!socket.loginBuilt) {
  33.           numberOfSocketsWithBuiltLoginScreens++;
  34.           socket.loginBuilt = true;
  35.  
  36.           if (numberOfSocketsWithBuiltLoginScreens === io.sockets.sockets.length) {
  37.             app.emit('all login screens built')
  38.           }
  39.         }
  40.       }
  41.  
  42.       socket.on('login credentials', socketLoginCredentials);
  43.      
  44.       function socketLoginCredentials(credentials) {
  45.         if (readyForLoginCredentials) {
  46.           socket.username = credentials.username;
  47.           socket.password = credentials.password;
  48.          
  49.           socket.emit('user stored data', socket.userData);
  50.         }
  51.       }
  52.     });
  53.    
  54.   }
  55.  
  56.  
  57. }
  58.  
  59. // ------------------- OR -------------------
  60.  
  61. socket.on('login screen built', socketLoginScreenBuilt);
  62.  
  63. function socketLoginScreenBuilt() {
  64.   // Notice how I'm not using a conditional here because I remove the 'login screen built' listener after this function is first ran. In that way I'll be certain that a socket won't trigger the important event logic multiple times
  65.   numberOfSocketsWithBuiltLoginScreens++;
  66.   socket.loginBuilt = true;
  67.  
  68.   if (numberOfSocketsWithBuiltLoginScreens === io.sockets.sockets.length) {
  69.     app.emit('all login screens built')
  70.   }
  71.  
  72.   socket.removeListener('login screen built', socketLoginScreenBuilt);
  73. }
  74.  
  75. socket.on('login credentials', socketLoginCredentials);
  76.  
  77. function socketLoginCredentials(credentials) {
  78.  
  79.   socket.username = credentials.username;
  80.   socket.password = credentials.password;
  81.  
  82.   socket.emit('user stored data', socket.userData);
  83.  
  84.   socket.removeListener('login credentials', socketLoginCredentials);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment