daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express  = require('express');
  2. var app      = express();
  3. var http     = require('http').Server(app);
  4. var shortId         = require('shortid');//import shortid lib
  5. var dgram = require('dgram');
  6. var socket = dgram.createSocket('udp4');
  7. app.use(express.static(__dirname));
  8.  
  9. var clients = [];//storage clients
  10. var clientLookup = {};// cliends search engine
  11. var sockets = {};//storage sockets
  12.  
  13. socket.on('message', function(message,rinfo) {
  14.  console.log('server got message: '+message+' from '+rinfo.address);
  15.    
  16.     var data = JSON.parse(message);//converte para json o conteudo da variavel message
  17.    
  18.     switch(data.id )
  19.     {
  20.      
  21.       case "PING":
  22.          console.log('[INFO] test ping received !!! ');
  23.          var response = "PONG"+','+"pong!!!";//formata os dados com o separador virgula para serem enviados de volta ao Sender
  24.          var msg = new Buffer(response);//bufering
  25.          console.log('send response to client');
  26.          
  27.          //Sending Messages Back to Sender "Socket.Emit"
  28.          socket.send(msg,
  29.                 0,
  30.                 msg.length,
  31.                 rinfo.port,//port of Sender
  32.                 rinfo.address//IP of Sender
  33.                     );
  34.                
  35.        
  36.       break;     
  37.       case "LOGIN":
  38.          console.log('[INFO] Player ' + data.name + ' connected!');
  39.          var currentUser = {
  40.             name:data.name,
  41.             id:shortId.generate(),
  42.             socketID:socket.id,
  43.             pass:data.pass,
  44.             port:rinfo.port,
  45.             address:rinfo.address
  46.          } //new user  in clients list
  47.        
  48.          clients.push(currentUser);//add currentUser in clients list
  49.          clientLookup[currentUser.id] = currentUser;//add client in search engine
  50.          sockets[currentUser.socketID] = socket;//add curent user socket
  51.        
  52.          console.log(" currentUser "+currentUser);
  53.          console.log('Total players: ' + clients.length);
  54.          //console.log('response: '+"LOGIN_SUCCESS "+','+currentUser.id+','+currentUser.name+','+currentUser.pass);
  55.          var response = "LOGIN_SUCCESS"+','+currentUser.id+','+currentUser.name+','+currentUser.pass;
  56.          var msg = new Buffer(response);
  57.          console.log('send response to client');
  58.          /* socket.send(msg,
  59.                 0,
  60.                 msg.length,
  61.                 rinfo.port,
  62.                 rinfo.address);
  63.                 */
  64.        
  65.          // tentativa de enviar em broadcast para todos os clientes "socket.emit.broadcast"
  66.          clients.forEach( function(i) {
  67.          console.log('socketID: '+i.socketID);
  68.               sockets[i.socketID].send(msg,
  69.                 0,
  70.                 msg.length,
  71.                 i.port,
  72.                 i.address);
  73.        
  74.          });
  75.       break;
  76.      
  77.       case "REGISTER":
  78.       break;
  79.       case "MOVE":
  80.       break;
  81.       case "ROTATE":
  82.       break;
  83.        
  84.    
  85.     }//END-SWITCH  
  86.    
  87.    
  88.    
  89.  
  90. });
  91.  
  92. var port = 5000;
  93.  
  94. socket.on('listening',function(){
  95.  
  96. var address = socket.address();
  97.  
  98. console.log('server listening on '+ address.address+':'+address.port);
  99. });
  100.  
  101. http.listen(process.env.PORT ||5000, function(){
  102.     console.log('listening on *:5000');
  103. });
  104. socket.bind(process.env.PORT||port);
  105. console.log("------- server is running -------");
RAW Paste Data
Top