Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io sync between only two clients

I'm trying to create a game where the browser in the desktop is one view and a mobile device is another view. You use the mobile device as the game controller to make characters in the desktop move. It is similar to chrome.com/supersyncsports.

Using socket.io, what is the best way to sync a mobile device to a desktop client? chrome.com/supersyncsports uses a code to sync the two clients. Are they using something similar to socket.io's namespacing or rooms, where the code is the name of room?

I'm looking for a way to sync the two clients and make sure data is only emitted to the correct device and connection and not all connections. What is the best of achieving this using socket.io?

like image 789
wwwuser Avatar asked Oct 21 '22 10:10

wwwuser


1 Answers

As film42 said, the code entry is probably the best way to do it, using something like this server side:

socket.on('room', function(room) {
    if(io.sockets.clients(room)>=2){
        socket.emit('error':"too Many connections")
    }
    else{
        socket.join(room);
    }
});

allows you to limit each room to two clients

like image 81
legacy Avatar answered Oct 24 '22 03:10

legacy