Advertisement
mjkaufer

Untitled

Aug 26th, 2014
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http'),  
  2. io = require('socket.io'),
  3. people = {},
  4. server = http.createServer(function(request, response) {  
  5.   response.writeHeader(200, {"Content-Type": "text"});
  6.   response.write("Good job.");
  7.   response.end();
  8. }).listen(8080);
  9. var socket = io.listen(server);
  10.  
  11. socket.on("connection", function (client) {  
  12.     client.on("join", function(name){
  13.         people[client.id] = name;
  14.         client.emit("update", "You have connected to the server.");
  15.         socket.sockets.emit("update", name + " has joined the server.")
  16.         socket.sockets.emit("update-people", people);
  17.         console.log("join");
  18.     });
  19.  
  20.     client.on("send", function(msg){
  21.         socket.sockets.emit("chat", people[client.id], msg);
  22.         console.log("send");
  23.     });
  24.  
  25.     client.on("disconnect", function(){
  26.         socket.sockets.emit("update", people[client.id] + " has left the server.");
  27.         delete people[client.id];
  28.         socket.sockets.emit("update-people", people);
  29.         console.log("disconnect");
  30.     });
  31. });
  32. console.log("Running");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement