paulon

Socket.IO Server

Feb 21st, 2012
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/lib/socket.io.js').listen(8080,"http://socket.local/");;
  2.  
  3. io.set("log level", 1);
  4.  
  5. io.sockets.on('connection', function (socket) {
  6. var address = socket.handshake.address;
  7. console.log("New connection from " + address.address + ":" + address.port + " ID: " + socket.id);
  8.  
  9. //first things first, we might want to send to client id to who ever is connecting to this server
  10. socket.emit('register',{client_id:socket.id});
  11.  
  12. //when some client is connecting we will send all the list of clients connected on this server
  13. io.sockets.emit('client list', {
  14. clients : socket.manager.connected
  15. });
  16.  
  17. socket.on('private message',function(data,fn){
  18. io.sockets.socket(data.socket_id).emit('private message',{msg: data.msg,from:socket.id});
  19. fn({success:true});
  20. });
  21. socket.on('disconnect', function () {
  22. io.sockets.emit('some user disconnected', {
  23. disconnected_id : socket.id
  24. });
  25. });
  26.  
  27. socket.on('send to all',function(data,fn){
  28. fn(true);
  29. //socket.emit('lobby message',{msg:sent_data}); //send only to self
  30. io.sockets.emit('lobby message',{msg:data,sender_id:socket.id}); //send to all
  31. //socket.broadcast.emit('lobby message',{msg:sent_data}); //send to all except who started it
  32. //socket.broadcast.json.send({msg:sent_data});
  33. });
  34. });
Advertisement
Add Comment
Please, Sign In to add comment