Advertisement
atm959

server.js

May 13th, 2019
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var net = require("net");
  2.  
  3. var packetTypes = {
  4.     REQ_ID: 1,
  5.     PLATFORM: 2,
  6.     LOGIN: 3,
  7.     ACCT_NO_EXIST: 4,
  8.     UN_INCORRECT: 5,
  9.     PW_INCORRECT: 6
  10. };
  11.  
  12. var platformTypes = {
  13.     WIIU: 1,
  14.     WII: 2,
  15.     PC: 3,
  16.     RASPBERRYPI: 4
  17. };
  18.  
  19. var nextID = 0;
  20.  
  21. var clients = []
  22.  
  23. net.createServer(function(socket){
  24.     socket.on("data",function(data){
  25.         console.log(data);
  26.         if(data[0] == packetTypes.REQ_ID){
  27.             var sendBuff = new Buffer([packetTypes.REQ_ID, nextID]);
  28.             socket.write(sendBuff);
  29.             nextID++;
  30.         } else if(data[0] == packetTypes.PLATFORM){
  31.             var clientID = data[1];
  32.             if(data[2] == platformTypes.WIIU){
  33.                 pltfrm = "Wii U";
  34.             } else if(data[2] == platformTypes.WII){
  35.                 pltfrm = "Wii";
  36.             } else if(data[2] == platformTypes.PC){
  37.                 pltfrm = "PC";
  38.             } else if(data[2] == platformTypes.RASPBERRYPI){
  39.                 pltfrm = "Raspberry Pi";
  40.             }
  41.             console.log("Client " + clientID + " Platform: " + pltfrm);
  42.         } else if(data[0] == packetTypes.LOGIN){
  43.             var clientID = data[1];
  44.             var bufferChars = data.toString();
  45.             var username = "";
  46.             for(var i = 0; i < 20; i++){
  47.                 username += bufferChars.charAt(i + 2);
  48.             }
  49.            
  50.             if(username != "atm959"){
  51.                 console.log("Account \"" + username + "\" doesn't exist!");
  52.                 var sendBuff = new Buffer([packetTypes.ACCT_NO_EXIST]);
  53.                 socket.write(sendBuff);
  54.             }
  55.            
  56.             console.log("Client " + clientID + " username: " + username);
  57.         }
  58.     });
  59.    
  60.     socket.on("error",function(e){
  61.         console.log("socket error:",e);
  62.         socket.destroy();
  63.     });
  64.     socket.on("disconnect",function(){
  65.         console.log("client sent disconnect event");
  66.         socket.destroy();
  67.     });
  68.     socket.on("close",function(){
  69.         console.log("socket closed");
  70.         socket.destroy();
  71.     });
  72. }).listen(11000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement