Skip to content

Commit

Permalink
disconnect websocket instead of sending LEAVE_ROOM.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jun 3, 2017
1 parent b9196c9 commit 61f8345
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
17 changes: 7 additions & 10 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ export class Client {
onError: Signal = new Signal();

constructor (url: string) {
let colyseusid = cookie.getItem('colyseusid');
if (colyseusid) {
this.id = colyseusid;
}

this.connection = new Connection(url);
this.connection.onmessage = this.onMessageCallback.bind(this);
this.connection.onclose = this.onCloseCallback.bind(this);
this.connection.onerror = this.onErrorCallback.bind(this);
}

onCloseCallback () {
this.onClose.dispatch();
}

onErrorCallback () {
this.onError.dispatch();
this.connection.onclose = (e) => this.onClose.dispatch();
this.connection.onerror = (e) => this.onError.dispatch();
}

join<T> (roomName: string, options: any = {}): Room<T> {
Expand Down
22 changes: 11 additions & 11 deletions src/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ export class Room<T=any> {
connect (connection: Connection) {
this.connection = connection;
this.connection.onmessage = this.onMessageCallback.bind(this);
this.connection.onopen = this.onOpenCallback.bind(this);
}

protected onOpenCallback (event) {
this.onJoin.dispatch();
this.connection.onopen = (e) => this.onJoin.dispatch();
this.connection.onclose = (e) => this.onLeave.dispatch();
}

protected onMessageCallback (event) {
Expand All @@ -54,9 +51,6 @@ export class Room<T=any> {
if (code == Protocol.JOIN_ERROR) {
this.onError.dispatch(message[2]);

} else if (code == Protocol.LEAVE_ROOM) {
this.onLeave.dispatch();

} else if (code == Protocol.ROOM_STATE) {
let state = message[2];
let remoteCurrentTime = message[3];
Expand All @@ -69,10 +63,12 @@ export class Room<T=any> {

} else if (code == Protocol.ROOM_DATA) {
this.onData.dispatch(message[2]);

} else if (code == Protocol.LEAVE_ROOM) {
this.leave();
}
}


setState ( state: T, remoteCurrentTime?: number, remoteElapsedTime?: number ): void {
this.state.set(state);
this._previousState = msgpack.encode( state )
Expand Down Expand Up @@ -112,14 +108,18 @@ export class Room<T=any> {
}

public leave (): void {
if (this.id >= 0) {
if (this.id) {
this.connection.close();
// this.connection.send([ Protocol.LEAVE_ROOM, this.id ]);
}
}

public send (data): void {
this.connection.send([ Protocol.ROOM_DATA, this.id, data ]);
if (this.connection.readyState === WebSocket.OPEN) {
this.connection.send([ Protocol.ROOM_DATA, this.id, data ]);
} else {
console.warn("Room", this.id, "is not connected.");
}
}

public removeAllListeners = (): void => {
Expand Down

0 comments on commit 61f8345

Please sign in to comment.