Haxe
You’re encouraged to use this SDK along with any Haxe Game Engine, such as: OpenFL, Kha, HaxeFlixel, Heaps, HaxePunk, etc.
Installation
Install colyseus
from haxelib:
Terminal
haxelib install colyseus
Usage
Connecting to server:
main.hx
import io.colyseus.Client;
import io.colyseus.Room;
var client = new Client('ws://localhost:2567');
Joining to a room:
See how to generate your RoomState
from State Synchronization → Client-side Callbacks
main.hx
client.joinOrCreate("room_name", [], RoomState, function(err, room) {
if (err != null) {
trace("JOIN ERROR: " + err);
return;
}
room.state.entities.onAdd(function(entity, key) {
trace("entity added at " + key + " => " + entity);
entity.onChange(function (changes) {
trace("entity has been changed");
});
})
room.state.entities.onChange(function(entity, key) {
trace("entity changed at " + key + " => " + entity);
})
room.state.entities.onRemove(function(entity, key) {
trace("entity removed at " + key + " => " + entity);
})
});
Other room events
Room state has been updated:
main.hx
room.onStateChange += function(state) {
// full new state avaialble on 'state' variable
}
Message broadcasted from server or directly to this client:
main.hx
room.onMessage("type", function (message) {
trace(client.id + " received on " + room.name + ": " + message);
});
Server error occurred:
main.hx
room.onError += function() {
trace(client.id + " couldn't join " + room.name);
}
The client left the room:
main.hx
room.onLeave += function() {
trace(client.id + " left " + room.name);
}
Running the demo project
The example
project can be compiled to html5
, neko
, cpp
, ios
, etc.
It uses the state_handler
room from the colyseus-examples project, which you can find here.
Compiling the demo project to html5
Terminal
git clone https://github.com/colyseus/colyseus-hx.git
cd colyseus-hx/example/openfl
lime build project.xml html5
You can see the demo project live here.
ios
target caveats
You may need to manually apply this patch in order to compile for iOS: HaxeFoundation/hxcpp@5f63d23
Last updated on