Skip to content

Commit

Permalink
allow to retrieve available room listing. closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Aug 21, 2018
1 parent 11380be commit 14a8840
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
14 changes: 14 additions & 0 deletions example/NyanCat/Source/Main.hx
Expand Up @@ -22,6 +22,20 @@ class Main extends Sprite {
this.client = new Client("ws://colyseus-examples.herokuapp.com");
this.room = this.client.join("state_handler");

// list available rooms for connection
haxe.Timer.delay(function() {
this.client.getAvailableRooms("state_handler", function(rooms, ?err) {
if (err != null) trace("ERROR! " + err);
for (room in rooms) {
trace("RoomAvailable:");
trace("roomId: " + room.roomId);
trace("clients: " + room.clients);
trace("maxClients: " + room.maxClients);
trace("metadata: " + room.metadata);
}
});
}, 3000);

this.client.onOpen = function() {
trace("CLIENT OPEN, id => " + this.client.id);
};
Expand Down
2 changes: 1 addition & 1 deletion haxelib.json
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"tags": ["multiplayer", "networking", "websockets", "netcode"],
"description": "Multiplayer Game Client for Haxe",
"version": "0.9.1",
"version": "0.9.2",
"classPath": "src/",
"releasenote": "Initial release, everything is working correctly.",
"contributors": ["endel"],
Expand Down
62 changes: 37 additions & 25 deletions src/io/colyseus/Client.hx
Expand Up @@ -26,8 +26,7 @@ class Client {
private var connectingRooms: Map<Int, Room> = new Map();
private var requestId = 0;

// private var roomsAvailableRequests: Map<String, RoomAvailable[] -> Void> = new Map();
// {[requestId: number]: (value?: RoomAvailable[]) => void}
private var roomsAvailableRequests: Map<Int, Array<RoomAvailable> -> Void> = new Map();

public function new (url: String) {
this.endpoint = url;
Expand Down Expand Up @@ -62,24 +61,28 @@ class Client {
return this.join(roomName, [ "sessionId" => sessionId ]);
}

// public function getAvailableRooms(roomName: String, callback: (rooms: RoomAvailable[], err?: string) => void) {
// // reject this promise after 10 seconds.
// const requestId = ++this.requestId;
// const removeRequest = () => delete this.roomsAvailableRequests[requestId];
// const rejectionTimeout = setTimeout(() => {
// removeRequest();
// callback([], 'timeout');
// }, 10000);

// // send the request to the server.
// this.connection.send([Protocol.ROOM_LIST, requestId, roomName]);

// this.roomsAvailableRequests[requestId] = (roomsAvailable) => {
// removeRequest();
// clearTimeout(rejectionTimeout);
// callback(roomsAvailable);
// };
// }
public function getAvailableRooms(roomName: String, callback: Array<RoomAvailable>->?String -> Void) {
// reject this promise after 10 seconds.
var requestId = ++this.requestId;

function removeRequest() {
return this.roomsAvailableRequests.remove(requestId);
};

var rejectionTimeout = haxe.Timer.delay(function() {
removeRequest();
callback([], 'timeout');
}, 10000);

// send the request to the server.
this.connection.send([Protocol.ROOM_LIST, requestId, roomName]);

this.roomsAvailableRequests[requestId] = function(roomsAvailable) {
removeRequest();
rejectionTimeout.stop();
callback(roomsAvailable);
};
}

public function close() {
this.connection.close();
Expand Down Expand Up @@ -158,12 +161,21 @@ class Client {
this.onError(cast message[2]);

} else if (code == Protocol.ROOM_LIST) {
// if (this.roomsAvailableRequests[message[1]]) {
// this.roomsAvailableRequests[message[1]](message[2]);
var requestId: Int = message[1];

if (this.roomsAvailableRequests.exists(requestId)) {
var callback = this.roomsAvailableRequests.get(requestId);
var roomsAvailable: Array<RoomAvailable> = cast message[2];

trace("Protocol.ROOM_LIST message => " + Std.string(message[2]));
trace("ROOM ID => " + roomsAvailable[0].roomId);
callback(cast (message[2]));

// } else {
// trace('receiving ROOM_LIST after timeout:' + message[2]);
// }
this.roomsAvailableRequests.remove(requestId);

} else {
trace('receiving ROOM_LIST after timeout:' + message[2]);
}

} else {
this.onMessage(message);
Expand Down

0 comments on commit 14a8840

Please sign in to comment.