1

if you have a a button on the page and you want to make sure the button cannot be clicked again before another socket clicks their button.

if socket a clicked i should disable the button until another socket sends a message that they clicked their button and back and fourth.

$(".buttonTurns").on("click", function(){
        socket.emit("turnbutton click")
})

and they dont input any names when they connect

jack blank
  • 4,275
  • 6
  • 33
  • 59
  • check this out http://stackoverflow.com/questions/4647348/send-message-to-specific-client-with-socket-io-and-node-js – VMcreator Aug 10 '15 at 06:10
  • I think that thread has the answer you've been looking for, the idea is to save the socket id of every client and you can access them via io.sockets.socket(savedSocketId).emit(...). – VMcreator Aug 10 '15 at 06:12

2 Answers2

0

You can set a UUID for each client (either client side javascript or server side javascript).

For a server side solution:

As @slebetman mentioned in the comments, socket.io has a built-in unique identifier for each socket.

as @slebetman suggested in his answer, to get the socket's unique ID () see his answer for more details):

// v0.6.x
var sid = socket.sessionId;    

// v0.7.x
var sid = socket.id;

For a client side solution:

If you set the unique ID on the client's side, than you probably need to create your own unique ID.

I took the following code from 6 revs and Briguy37's answer to a question here on this site:

function generateUUID(){
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};

The randomness should be strong enough for all your clients to have a unique identifier using this ID generator. This should work with the code you wanted to use regarding your question here.

Community
  • 1
  • 1
Myst
  • 16,507
  • 2
  • 40
  • 61
0

Every socket.io session is automatically assigned a unique string as an id. On the server you can get it from:

socket.id

If you are inside a session (that is, the connection that returns the socket object) you can send messages back to the client by simply doing:

socket.emit('event name',data);

But if you want to send a message to a different session then you need to do:

io.sockets.socket(socket_id).emit('event name',data);

If you want to send a message to all connected sessions then just do:

io.emit('event name', data); // broadcast

If your application have state that needs to be managed, then one technique is to store them in an object using the socket id as the key:

var buttonState = {}

io.on('connection',function('socket'){
    // default button to off when socket connects:
    bottonState[socket.id] = 'off';

    socket.on('on button',function(){
        bottonState[socket.id] = 'on';
    })

    socket.on('off button',function(){
        bottonState[socket.id] = 'off';
    })
})

Now that you can manage the individual state for each individual client on the server you can use that to communicate them to other clients.

slebetman
  • 93,070
  • 18
  • 116
  • 145