Skip to content

Instantly share code, notes, and snippets.

@MikeShi42
Created August 13, 2018 17:17
Show Gist options
  • Save MikeShi42/49cd7051413758a03dcdd55adb42810a to your computer and use it in GitHub Desktop.
Save MikeShi42/49cd7051413758a03dcdd55adb42810a to your computer and use it in GitHub Desktop.
competitive 2048 index js pt 2
const connections = [null, null];
// Handle a socket connection request from web client
io.on('connection', function (socket) {
// Find an available player number
let playerIndex = -1;
for (var i in connections) {
if (connections[i] === null) {
playerIndex = i;
}
}
// Tell the connecting client what player number they are
socket.emit('player-number', playerIndex);
// Ignore player 3
if (playerIndex == -1) return;
connections[playerIndex] = socket;
// Tell everyone else what player number just connected
socket.broadcast.emit('player-connect', playerIndex);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment