This is a simple communication library for the precursors server. It has been designed to make things as straightforward as possible, using things that should be very familiar to node developers.
Simply install the
precursors-client package from npm:
$ npm install --save precursors-client
First things first, you need to pick which client type you want to use. You choices are as follows:
Note: At the moment, the only supported client is the Game Client. Once service client support is added to precursors server, you will be able to use it.
Regardless of which client you choose, you connect the same way:
var prelib = ;var client = 'localhost' 6006;client;
connect method of the client returns a promise, so you can easily chain it with other promises.
As you will notice in the example, we use a custom error
RequestDenied to indicate that the request (in this case,
the login) was denied.
Once you have a connection to the server, you then will need to create a channel object:
var channel = client;
Now that you have a channel, you can listen for events. We emit them based on their 'type' in precursors server.
If, however, you need to listen on a more low level, you can listen for the 'event' event, which is fired internally. This gives you the ability to handle the message before it is unwrapped or routed.
Sending an event is simple:
// Send a command eventchannelevent type: 'command' name: 'remove' ;// Send a command event over TCPchannelevent type: 'command' name: 'remove' 'tcp';
By default, all events and requests are sent over SSL. However, this isn't desirable for things like input events. This
is why we allow for you to send events over the TCP channel by passing the string
'tcp' as the second parameter.
Working with requests is made easy, using promises. Like events, You send your message and (optionally) the transport as a string:
var loginMsg =type: 'login'clientName: 'My Client'clientType: 'game'version: '0.1.0'user: 'test'password: 'pass1234'key: 'random 16bit Key'vector: 'random 16bit IV';// Send the request and handle the responsechannel;
As you can see,
request returns a promise that is only resolved if the request is confirmed. Otherwise you will need
to catch the custom
RequestDenied error. Denied requests should have a human-readable reason in the
of the message.