Created
August 13, 2018 17:17
-
-
Save MikeShi42/49cd7051413758a03dcdd55adb42810a to your computer and use it in GitHub Desktop.
competitive 2048 index js pt 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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