gamelobby
ES6 JS classes for Game Lobbies with friend lobbies, heartbeats and disconnect handlers
Installation
npm install gamelobby --save
Usage
This is a Game Lobby implementation I made for http://x64projects.tk/WikiRace
Main features:
- Friend lobbies by generating uuidv4 hashes for lobby names.
- Disconnect handlers for BOTH timeouts and players closing the page due to the heartbeats.
- Object-oriented design for overriding methods.
- Event-based callbacks for game lobbies
- Websocket based (using SailsJS, but can be overridden) for maximum speed.
LobbyManager inherits from node's EventEmitter, and thus allows you to subscribe/publish events
of your own choosing as well as add hooks to the existing ones.
{ ... // Events and handlers for LobbyManager. Hook your own with .on("") for these same events // NOTE: COMMENTED EVENTNAMES HERE ARE VALID. LobbyManager just doesn't need to hook them // Each of these below events (LOBBY_CREATED/LOBBY_DESTROYED) are also broadcasted to every // player of a lobby the event corresponds to via websockets. thisEVENTS = //LOBBY_CREATED: (lobby) => { }, LOBBY_DESTROYED: this_onLobbyDestroy // (lobby) => { }, //PLAYER_ADDED: (lobby, player) => { }, // Fires on player add. Does NOT fire on lobby creation (as first player joins) GAME_START: this_onLobbyGameStart // (lobby) => { }, //GAME_OVER: (lobby) => { }, //USER_DISCONNECT: (lobby, disconnectedList) => { }, // one or more players disconnected (missed heartbeats) //GAME_OVER_DUE_TO_DISCONNECTS: (lobby) => { }, // game cannot continue due to disconnections //TOOK_WAY_TOO_LONG: (lobby) => { }, // Game hit maximum time allowed to run ; ...};
Incorporate gamelobby into a web app or networked application by creating lobbies and adding players.
const gamelobby = ;const lobbyManager = ; // Get a lobby with specified config.let lobbyConfig = mode: "BATTLE" ; // other ideas: { isFriendLobby: true, isTeamGame: true }let lobby = lobbyManager; // ONLY gets lobbies where mode is "BATTLE" // If no lobbies exist with these requirements, create oneif !lobby lobby = lobbyManager; // Add player to this lobby - this will emit GAME_START if the required number of players is reachedlet playerConfig = username: "billy" userId: 4 character: "turtle" ;lobby; // is synchronous, will emit PLAYER_ADDED // PLAYER_ADDED event will send all players the playerConfig for each player.// lobby.playerList will be an array of Player objects with .playerConfig accessible // Add GAME_START handler to determine the teams of a gamelobbyManager; // Add GAME_OVER handler to update player ranks upon completionlobbyManager;
Lobbies automatically check for disconnections by receiving heartbeats every few seconds.
Have your web app send heartbeats to another endpoint and update the lobby.
Timeouts result in USER_DISCONNECT being emitted.
// Update heartbeat for accurate disconnectionslet lobby = lobbyManager; // req.socket is your socket identifier (in sails at least)if !lobby return res; // player not in any lobbylobby;
Used in tandem with my LobbyClient.js on the front end (to be released in future) helps a lot.
More features available, read the code to suit your needs.
TODO:
Add easier Redis support, maybe as a subclass.