Used to communicate with ManiaPlanet servers.
Note: The API may, or may not change!
npm install gbxremote
Look in /examples/ for all examples.
The following examples expects that
var gbxremote = require('gbxremote').
To connect to a server, use
var client = gbxremote.createClient(port, [host], [callback]);
Examples of ways to connect to the server:
// Connect with port onlyvar client = gbxremotecreateClient5000;// Connect with port and hostnamevar client = gbxremotecreateClient5000 'localhost';// Connect with port and ipvar client = gbxremotecreateClient5000 '127.0.0.1';// Connect with port only, and a callbackvar client = gbxremotecreateClient5000// This callback is called both on connect and on error so we should check it.if errconsole.error'Could not connect to server:' err;elseconsole.log'Connection to server was successfull! Ready to send queries..';;// Connect with port, ip and a callbackvar client = gbxremotecreateClient5000 '127.0.0.1'// Callback...;
Queries are sent to the server by calling
client.query(method, [params], [callback]);
Queries before the connect event has been emitted will be queued and sent on connect!
var client = gbxremotecreateClient5000;clienton'connect'// GetVersion does not take any params.clientquery'GetVersion'if errconsole.error'Error when querying server:' err;elseconsole.log'Server version:' resjoin', ';;// GetPlayerInfo takes 2 parameters, 1 optional.// GetPlayerInfo(string login, [int compatibility])clientquery'GetPlayerInfo' 'minigod'if errconsole.error'Error getting player info:' err;elseconsole.log'Player info:';console.logres;;;
Emitted when connection to the server is successfull.
Ready to receive queries!
var client = gbxremotecreateClient5000;clienton'connect'console.log'Connection successfull! Lets do some queries!';clientquery'GetVersion'if errconsole.logerr;elseconsole.logres;;;
If there is a problem connecting, the 'connect' event will not be emitted, the 'error' event will be emitted with the exception.
- Socket errors (host is not listening on that port, loose connection, etc.)
- Handshake fails (host is listening on that port, but its not a ManiaPlanet (GbxRemote 2) server)
var client = gbxremotecreateClient80;clienton'error'console.error'Connection failed: ' + err;;
EnableCallbacks(true) to the server, it will send you callbacks when stuff happend on the server.
var client = gbxremotecreateClient5000;clienton'connect'clientquery'SetApiVersion' '2012-06-19';clientquery'EnableCallbacks' true;;clienton'callback'console.log"Callback from server: %s - %d params" method paramslength;// This would be the typical place to have a switch statement. Please dont do that. Use the events, as shown below.;
Callbacks will also emit separate events for each method. It's hard to explain. Learn from example:
var client = gbxremotecreateClient5000;clienton'connect'// Before enabling callbacks, make sure you set the latest API.clientquery'SetApiVersion' '2012-06-19';clientquery'EnableCallbacks' true;;// ManiaPlanet.PlayerConnect(string Login, bool IsSpectator);clienton'ManiaPlanet.PlayerConnect'console.log'%s just joined as a %s' params0 params1 ? 'spectator' : 'player';;// ManiaPlanet.PlayerDisconnect(string Login);clienton'ManiaPlanet.PlayerDisconnect'console.log'%s left the server' params0;;
These events can basically take over the big switch statements that is normal in todays server controllers.
Emitted once the socket is fully closed. The argument had_error is a boolean which says if the socket was closed due to a transmission error.
var client = gbxremotecreateClient5000;clienton'connect'// Connected...// Do stuff?// Disconnectclientterminate;;clienton'close'console.log'Connection to the server has been closed';;
This section does not currently apply, because tests are not being maintained atm
Note: Tests have not been changed since fork, hence will not pass.
TODO: Fix tests - Figure out how to do it with travis (and in general), since we need a running ManiaPlanet server to run tests - and we need to know exactly what the server will return.
To run the test suite:
If submitting a bug fix, please update the appropriate test file too.
Released under the MIT license. See the LICENSE file for the complete wording.