Gordon Server
Gordon is a lightweight server for developing multiuser apps and games with HTML5 and Adobe Flash/Air. It uses a lean binary protocol to exchange data.
Install
With npm do:
npm install gordon-server
See also gordon-client and gordon-examples.
Structure
Session
The session is the highest object in the Gordon hierarchy. Every session is completely independent from other sessions. The session contains all other Gordon objects like rooms, users and dataObjects.
Room
A session can be divided in any number of rooms. Rooms are where the clients interact. Every room is limited to a maximum number of clients/users and can be optionally locked by a password.
User
Every client connected to Gordon and joined to a session is represented as a User. Users can interact with each other by updating their dataObjects, sending chat or custom messages.
DataObjects
DataObject are used to synchronize states between the connected clients. DataObjects live inside a room. Every dataObject consists of key/value pairs, where keys must be unique integers (per DataObject) and the values must be Buffers. Every user has his own dataObject. Additional dataObjects could be created by a client or by the server.
DataObject could be used to represent objects like e.g. bots, windows, laser shots, grenades, barrels, text blocks, cars etc...
Usage
var gordon = ;gordon;gordon;gordon;var session = gordon;var lobby = session;var room1 = session;
Logic Factory
You could assign a logic factory
to a session.
The assigned function is called when a room has been created internally.
This could be used to modify the room's properties or e.g. attach a custom room logic accordingly by returning a 'room logic object'.
See room logic
for more infos.
var gordon = ;//require a custom logic classvar Gamelogic = ;gordon;gordon;gordon;var session = gordon;//The rooms will be created on the fly, when users want to joinsessionautoRoomCreate = true;session {var gamelogic;if roomid == 'room1'gamelogic = room 0x800080;//by default rooms will be removed automatically after 2 sec if empty//setting the persistent flag to true will prevent thisroompersistent = true;roomname = 'Room#1';return gamelogic;if roomid == 'room2'gamelogic = room 0xFF8000;roompersistent = true;roomname = 'Locked Room';roompassword = '12345';return gamelogic;if roomid == 'room3'gamelogic = room 0x80FF00;roompersistent = true;return gamelogic;};
Room Logic
Every room can have its own custom room logic. See the examples for use cases.
var Gamelogic = ; var session = gordon;var lobby = session; //create the logic module and attach it to the roomlobbylogic = lobby;
The logic object could also define the following methods to modify the standard protocol flow:
Call the passed in proceed function to continue with the standard protocol flow.
//modifying a join requestthis { //alter the user's dataObject values userdataObject; userdataObject; //simulating a long running e.g. database query ;}; //modify a change room requestthis { //don't allow users to change to room3 if newRoomid == 'room3' ; else ; }; //modifying a chat messagep { //target could be a user or an object with user.id/user key/value message = message; ;}; //modifying a custom messagep { if buffer == 0xc000 var b = 4; b; ; };
DataObjects
DataObject are used to synchronize states between the connected clients. DataObjects live inside a room. Every DataObject consists of key/value pairs, where keys must be unique integers (per DataObject) and values must be Buffers.
Every connected user automatically has its own DataObject. Additional DataObjects can be created with the room.createDataObject()
method.
var room = session; var values = {};values0 = 1;values1 = 2;values2 = 2;values3 = 1;values4 = 4; //write the initial valuesvalues0;values1;values2;values3;values4; //create and broadcast the new DataObjectroom;
The server also can update and broadcast values:
var dataObject = userdataObject;dataObject;dataObject;dataObject; //broadcast values to dataObject's roomdataObject;