Skip to content

Commit

Permalink
allow to call Room.leave() before connection is accepted.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jan 9, 2018
1 parent d42d194 commit d91ddfc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/colyseus.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.8.1-alpha.1",
"version": "0.8.2-alpha.2",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand Down
20 changes: 16 additions & 4 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,19 @@ export class Client {
join<T> (roomName: string, options: any = {}): Room<T> {
options.requestId = ++this.joinRequestId;

this.connectingRooms[ options.requestId ] = new Room<T>(roomName);
const room = new Room<T>(roomName);

// remove references on leaving
room.onLeave.addOnce(() => {
delete this.rooms[room.id];
delete this.connectingRooms[options.requestId];
});

this.connectingRooms[ options.requestId ] = room;

this.connection.send([Protocol.JOIN_ROOM, roomName, options]);

return this.connectingRooms[ options.requestId ];
return room;
}

/**
Expand All @@ -80,16 +88,20 @@ export class Client {
let requestId = message[2];
let room = this.connectingRooms[ requestId ];

if (!room) {
console.warn("colyseus.js: client left room before receiving session id.");
return;
}

this.rooms[room.id] = room;

room.id = message[1];
room.connect(new Connection(`${ this.hostname }/${ room.id }?colyseusid=${ this.id }`));
room.onLeave.add(() => delete this.rooms[room.id]);

delete this.connectingRooms[ requestId ];

} else if (code == Protocol.JOIN_ERROR) {
console.error("server error:", message[2]);
console.error("colyseus.js: server error:", message[2]);

// general error
this.onError.dispatch(message[2]);
Expand Down
5 changes: 4 additions & 1 deletion src/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ export class Room<T=any> extends DeltaContainer<T & any> {
}

public leave (): void {
if (this.id) {
if (this.connection) {
this.connection.close();

} else {
this.onLeave.dispatch();
}
}

Expand Down

0 comments on commit d91ddfc

Please sign in to comment.