Skip to content

Commit

Permalink
introduce room.sessionId. colyseus/colyseus#43
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jun 5, 2017
1 parent 61f8345 commit 859fc76
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"msgpack-lite": "^0.1.20",
"signals.js": "^1.0.0",
"tiny-emitter": "^1.1.0",
"websocket.js": "^0.1.9"
"websocket.js": "^0.1.10"
},
"devDependencies": {
"@types/chai": "^3.4.34",
Expand Down
26 changes: 15 additions & 11 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import { Room } from "./Room";
import { Connection } from "./Connection";

export class Client {
id?: string;

connection: Connection;
room: Room;
public id?: string;

// signals
onOpen: Signal = new Signal();
onMessage: Signal = new Signal();
onClose: Signal = new Signal();
onError: Signal = new Signal();
public onOpen: Signal = new Signal();
public onMessage: Signal = new Signal();
public onClose: Signal = new Signal();
public onError: Signal = new Signal();

protected connection: Connection;
protected room: Room;
protected rooms: {[id: string]: Room} = {};

constructor (url: string) {
let colyseusid = cookie.getItem('colyseusid');
Expand Down Expand Up @@ -51,9 +52,12 @@ export class Client {
this.onOpen.dispatch();

} else if (code == Protocol.JOIN_ROOM) {
this.room.id = message[1];
this.room.connect(new Connection(`${ this.connection.url }/${ this.room.id }`));
// this.connection.close();
let room = this.room;
room.id = message[1];
room.connect(new Connection(`${ this.connection.url }/${ this.room.id }`));
room.onLeave.add(() => delete this.rooms[room.id]);

this.rooms[room.id] = room;

} else if (code == Protocol.JOIN_ERROR) {
console.error("server error:", message[2]);
Expand Down
1 change: 1 addition & 0 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Connection extends WebSocketClient {
return super.send( msgpack.encode(data) )

} else {
console.warn(`colyseus.js: trying to send data while in ${ this.ws.readyState } state`);

// WebSocket not connected.
// Enqueue data to be sent when readyState == OPEN
Expand Down
15 changes: 7 additions & 8 deletions src/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Connection } from "./Connection";
export class Room<T=any> {
public id: number;
public name: string;
public sessionId: string;

public state: DeltaContainer<T & any> = new DeltaContainer<T & any>({});

Expand Down Expand Up @@ -40,15 +41,18 @@ export class Room<T=any> {
connect (connection: Connection) {
this.connection = connection;
this.connection.onmessage = this.onMessageCallback.bind(this);
this.connection.onopen = (e) => this.onJoin.dispatch();
this.connection.onclose = (e) => this.onLeave.dispatch();
}

protected onMessageCallback (event) {
let message = msgpack.decode( new Uint8Array(event.data) );
let code = message[0];

if (code == Protocol.JOIN_ERROR) {
if (code == Protocol.JOIN_ROOM) {
this.sessionId = message[1]
this.onJoin.dispatch();

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

} else if (code == Protocol.ROOM_STATE) {
Expand Down Expand Up @@ -110,16 +114,11 @@ export class Room<T=any> {
public leave (): void {
if (this.id) {
this.connection.close();
// this.connection.send([ Protocol.LEAVE_ROOM, this.id ]);
}
}

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

public removeAllListeners = (): void => {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2826,9 +2826,9 @@ webpack@^2.1.0-beta.25:
webpack-sources "^0.1.0"
yargs "^6.0.0"

websocket.js@^0.1.9:
version "0.1.9"
resolved "https://registry.yarnpkg.com/websocket.js/-/websocket.js-0.1.9.tgz#3a9bbd65fe93835bc756ecada57f0961dbdcc53e"
websocket.js@^0.1.10:
version "0.1.10"
resolved "https://registry.yarnpkg.com/websocket.js/-/websocket.js-0.1.10.tgz#6bad57a24d0210561018294b49234d21fa9fd7ee"
dependencies:
backoff "^2.4.1"

Expand Down

0 comments on commit 859fc76

Please sign in to comment.