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

The Client class represents a single connection from a user client. It can be put into a ClientManager instance, and is the primary way to send data to and receive data from remote players.

Instance

new Client(net.Socket socket) -> client

Create a new client instance. This is almost never necessary to call manually, as the Server will pass instantiated Client instances into its callback when a new user connects.

.send([String command,] Object data)

Send a command object to the client over the network. If command is defined, it will be automatically included in the command object that is sent over the network. Otherwise, you can just pass .send a fully constructed command object to send:

client.send('message', {content: 'hello world'});
client.send({command: 'message', content: 'hello world'});
.set(String key, Mixed value)

Save data to this client instance. This data exists on the server only and is not sent to the client. Use this to store information locally about the client.

.get(String key)

Gets data stored with .set.

.on(String eventName, Function callback)

Registers an event handler on the underlying net.Socket object in this client.

.onData(Function callback)

Specify a callback function to pass any data received from this client into. This is primarily used by ClientManager instances, but you can specify extra handlers manually.

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

Register a function to run when this instance receives a command object. The function will be passed the the command object.

client.addCommandListener('register', function(client, data) {
    console.log(client.clientId + ' has registered');
});
.removeCommandListener(String command, Function<Object data> handler)

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

.setTickMode(Boolean onOff)

Turn on or off Tick Mode for this Client. When tick mode is on, commands passed into .send() will be stored until .tick() is called and then sent together.

.tick()

Sends all queued commands since the last time .tick was called when tick mode is on for this client.

Static

Client.getObjectFromRaw(Buffer socketData)

Converts data from a socket buffer into a JavaScript data object.