Advertisement
Guest User

Untitled

a guest
Aug 20th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is required to run the server
  2. // here we define the port the messages are sended though.
  3. // in this case, it is port 8080, you can define it to every port you want.
  4. //but take note, some ports are already in use, then it will throw in an error message.
  5. var io = require("socket.io").listen(8080);
  6. //a variable for our "automatch making"
  7. TmpRoom="wait";
  8. //if a client connects to this, it performs this whole, big function.
  9. io.sockets.on("connection", function (socket) {
  10.  
  11.      // when the client emits 'adduser', this listens and executes
  12.      socket.on("joint", function(data){
  13.           // checks, if no other waiting player exists,
  14.           if(TmpRoom=="wait")
  15.           {
  16.                //if yes, then it perform this
  17.                // it sais TmpRoom = data, that is the username that is data
  18.                /*That means, TmpRoom is not anymore = wait! so the next one that
  19.                wants to join here, go into the next loop, the "else" loop */
  20.                TmpRoom = data;
  21.                //then it creates, and joins a room, that is called after the users username
  22.                socket.join(TmpRoom);
  23.                //Now it sends the message, you,playersname
  24.                socket.send("you," + TmpRoom);
  25.                // and now, it ends the loops.
  26.           }
  27.          
  28.           else
  29.           {
  30.                //Joins the Temporary Room, to the enemy player
  31.                socket.join(TmpRoom);
  32.                //Then it sends, the name of the room.
  33.                socket.send("Room," + TmpRoom);
  34.                //now, the interesting part. It not just sends a data to ONE client,
  35.                //but to all clients in the room. To all clients in the TmpRoom.
  36.                // the data that it sends, are the currently room name.
  37.                io.sockets.in(TmpRoom).send("enemy," + TmpRoom);
  38.                //and the username from the other player.
  39.                io.sockets.in(TmpRoom).send("enemy," + data);
  40.                //now it again sets the TmpRoom = wait, so the next that tries to join,
  41.                //again, is going to the upper loop, and creating its own room.
  42.                //Rooms are automatically "destroyed" when the Host, leaves the room.
  43.                TmpRoom = "wait";
  44.           }
  45.                
  46.      });
  47.      //if the socket, gets a custom function from C2, called Message with some data(the room name),
  48.      //then it performs this:
  49.      socket.on("message", function(data){
  50.           //now it is in the loop, and if the server gets a event called Chatmessage,
  51.           //probably with a string message, then it performs this:
  52.           socket.on("chatmessage", function(data2){
  53.                //Sends a chat message in the room.
  54.                //it sends data to all clients in the room. that
  55.                //the sending client defined in his "message" function.
  56.                io.sockets.in(data).send("chatmessage," + data2);
  57.           });
  58.           //that is pretty much the same and don´t require any explanation
  59.           // i hope.
  60.           socket.on("GameSettings", function(data2){
  61.                //Sends the Game settings, x,y coordinate, String, everything!
  62.                io.sockets.in(data).send("GameSettings," + data2);
  63.           });
  64.      });
  65.      // when the user disconnects. perform this:
  66.      socket.on("disconnect", function(data){
  67.           //on disconnecting, it leaves the room.
  68.           socket.leave(data);
  69.      });
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement