Skip to content
Michael Barrett edited this page Sep 12, 2020 · 7 revisions

The ClientManager class is a wrapper for a collection of Client objects. It manages data coming in from client users, and can broadcast data back out. It is the main method of managing data across clients. Typically, you will want to pass Client objects into a ClientManager in the Server connection callback:

var server = new Server(function(client) {
  gameManager.addClient(client);
});

A ClientManager is usually used to handle Client objects in various states. For example, you may have a lobbyManager which handles users that are in a game lobby, or a gameManager that handles users in-game.

Instance

new ClientManager() -> ClientManager

Creates a new client manager and prepares it for receiving clients.

.getClients() -> Array

Returns an array containing every Client instance in this ClientManager.

.getClientCount() -> Number

Returns the number of clients currently in this ClientManager.

.set(String key, Mixed value)

Saves data to this instance to be used later.

.get(String key) -> Mixed

Returns data saved with .set()

.addClient(Client client)

Adds the given Client to this ClientManager

.removeClient(Number clientId) -> Client

Removes the client with the matching clientId from this instance and returns the client it removed, or undefined if no client was found with the given clientId

.disconnectClient(Client client, String reason = '')

Destroys and removes the given client, firing a clientDropped event. Useful for non-graceful client drops.

.broadcast([String command,] Object data)

Sends the given command to all Client objects in this instance. As with Client.send, specifying the command string is optional if the data object is a fully formed command object.

.handleIncomingCommand(Client client, Object data)

Runs the command handler for the given command data object with the given Client as the sending client. This function is meant for internal use, but could be used manually if you really wanted to.

.addCommandListener(String command, Function<Client client, Object data> handler)

Register a function to run when this instance receives a command object from a Client. The function will be passed the client that sent the command, and the command object.

clientManager.addCommandListener('chat', function(client, data) {
    console.log(client.clientId + ' says: ' + data.message);
});
.removeCommandListener(String command, Function<Client client, Object data> handler)

Removes the given function that has been registered as a command listener.

.on(String eventName, Function handler)

Register a function to run for a given ClientManager event. See ClientManager events for a full list of events you can register on.

.setTickMode(Boolean onOff)

Enables or disables Tick Mode for this ClientManager. Disabling tick mode with this method will stop ticking.

.setTickRate(Number newTickRate)

Set the rate that this ClientManager should tick at when tick mode is enabled and running. newTickRate is the time in milliseconds between ticks. Default tick rate is 1000 / 60 (60 ticks per second)

.startTicking()

Begins ticking once tick mode is enabled with .setTickMode

stopTicking()

Stops ticking when ticking is running

tick()

Runs a tick for all Client objects in this ClientManager

Events

commandReceived -> client, data

Fires when a command is received from any client in this ClientManager. Passes the triggering client object and the data sent by that client (a command object) to the handler function.

clientAdded -> client

Fires when a client is added into this ClientManager. Passes the added client to the handler.

clientDropped -> {client, reason}

Fires when a client closes its connection to the server. Passes an object with client and reason as keys to the handler. This is different from when a client is removed from this ClientManager, as it only fires when the client itself has ended the connection (typically meaning the player has quit the game)

tick

Fires when a tick happens when Tick Mode is enabled and running.