First of all, how can I make sure that there are only 2 players who can access an instance of the game?
Create an array of instance objects.
When a new player joins, either creates a new instance, or sets them as player1, or adds them to the existing instance as a second player.
var instances = []; function Instance () { return { name = 'game' + instances.length + 1, gameVariables = defaults, player1 = null, player2 = null, player1UUID = UUID(), player2UUID = UUID() } }
Is it possible for them to reconnect if they lose their connection?
If you send each player a UUID on the first connection, you can use it to authenticate when reconnecting.
How to send a message from server to client?
client.send ({gameState: gameState ()});
if you saved the client in the object: instances[ 'game1' ].player1.send( data );
Am I doing all this with sockets?
I would consider all dynamic interactions with web sockets.
Do I return a web page with the updated state of the game (cards, bets, etc.) every turn?
I would not send html via web sockets. Instead, send json and use the client-side template to display it.
source share