In the previous post, I talked about how to set up Quintus and Node.js for a simple single player web game. Unfortunately, because we’re coding up a game of tag, keeping our game single player doesn’t make all that much sense.
Socket.io
Listening for connections/disconnects
We’ll write the code to monitor connections and disconnects on our server-side app.js
:
|
|
Lines 6-7: Declaring variables that are used to track the number of players and assign a unique id for each player respectively.
Lines 12-15: We wrap these socket emit events in a timeout because if a connection event was fired to the user right when they started connecting to the webpage, they wouldn’t have loaded the proper javascript files yet and the event wouldn’t do anything. A 1.5 second delay on connection gives a nice buffer time.