Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/lib/socket.io.js').listen(8080,"http://socket.local/");;
- io.set("log level", 1);
- io.sockets.on('connection', function (socket) {
- var address = socket.handshake.address;
- console.log("New connection from " + address.address + ":" + address.port + " ID: " + socket.id);
- //first things first, we might want to send to client id to who ever is connecting to this server
- socket.emit('register',{client_id:socket.id});
- //when some client is connecting we will send all the list of clients connected on this server
- io.sockets.emit('client list', {
- clients : socket.manager.connected
- });
- socket.on('private message',function(data,fn){
- io.sockets.socket(data.socket_id).emit('private message',{msg: data.msg,from:socket.id});
- fn({success:true});
- });
- socket.on('disconnect', function () {
- io.sockets.emit('some user disconnected', {
- disconnected_id : socket.id
- });
- });
- socket.on('send to all',function(data,fn){
- fn(true);
- //socket.emit('lobby message',{msg:sent_data}); //send only to self
- io.sockets.emit('lobby message',{msg:data,sender_id:socket.id}); //send to all
- //socket.broadcast.emit('lobby message',{msg:sent_data}); //send to all except who started it
- //socket.broadcast.json.send({msg:sent_data});
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment