Skip to content

Commit

Permalink
allow to enqueue room messages before connecting. include change.prev…
Browse files Browse the repository at this point in the history
…iousValue on 'replace' operations.
  • Loading branch information
endel committed Jul 22, 2018
1 parent e7f5f2d commit 40a4108
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist/colyseus.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.9.11",
"version": "0.9.12",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand Down Expand Up @@ -29,8 +29,8 @@
"dependencies": {
"@gamestdio/clock": "^1.1.0",
"@gamestdio/signals": "^1.0.0",
"@gamestdio/state-listener": "^3.0.0",
"@gamestdio/websocket": "^0.2.5",
"@gamestdio/state-listener": "^3.1.0",
"@gamestdio/websocket": "^0.2.8",
"fossil-delta": "^1.0.0",
"notepack.io": "^2.1.3"
},
Expand Down
8 changes: 4 additions & 4 deletions src/Client.ts
Expand Up @@ -77,7 +77,7 @@ export class Client {
protected connect(colyseusid: string) {
this.id = colyseusid || '';

this.connection = this.createConnection();
this.connection = new Connection(this.buildEndpoint());
this.connection.onmessage = this.onMessageCallback.bind(this);
this.connection.onclose = (e) => this.onClose.dispatch(e);
this.connection.onerror = (e) => this.onError.dispatch(e);
Expand All @@ -90,7 +90,7 @@ export class Client {
};
}

protected createConnection(path: string = '', options: any = {}) {
protected buildEndpoint(path: string = '', options: any = {}) {
// append colyseusid to connection string.
const params = [`colyseusid=${this.id}`];

Expand All @@ -101,7 +101,7 @@ export class Client {
params.push(`${name}=${options[name]}`);
}

return new Connection(`${this.hostname}/${path}?${params.join('&')}`);
return `${this.hostname}/${path}?${params.join('&')}`;
}

/**
Expand Down Expand Up @@ -129,7 +129,7 @@ export class Client {
room.id = message[1];
this.rooms[room.id] = room;

room.connect(this.createConnection(room.id, room.options));
room.connect(this.buildEndpoint(room.id, room.options));
delete this.connectingRooms[ requestId ];

} else if (code === Protocol.JOIN_ERROR) {
Expand Down
15 changes: 7 additions & 8 deletions src/Connection.ts
@@ -1,25 +1,26 @@
import WebSocketClient from '@gamestdio/websocket';
import * as msgpack from './msgpack';

import { Protocol } from './Protocol';

export class Connection extends WebSocketClient {

private _enqueuedCalls: any[] = [];

constructor(url, query: any = {}) {
super(url);

this.binaryType = 'arraybuffer';
constructor(url, autoConnect: boolean = true) {
super(url, undefined, { connect: autoConnect });
}

public onOpenCallback(event) {
super.onOpenCallback();

this.binaryType = 'arraybuffer';

if (this._enqueuedCalls.length > 0) {
for (const [method, args] of this._enqueuedCalls) {
this[method].apply(this, args);
}

// clear enqueued calls.
this._enqueuedCalls = [];
}
}

Expand All @@ -28,8 +29,6 @@ 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
this._enqueuedCalls.push(['send', [data]]);
Expand Down
12 changes: 5 additions & 7 deletions src/Room.ts
Expand Up @@ -5,10 +5,8 @@ import { StateContainer } from '@gamestdio/state-listener';
import * as fossilDelta from 'fossil-delta';
import * as msgpack from './msgpack';

import { Client } from './Client';
import { Connection } from './Connection';
import { Protocol } from './Protocol';
import { setItem } from './Storage';

export interface RoomAvailable {
roomId: string;
Expand Down Expand Up @@ -43,21 +41,21 @@ export class Room<T= any> extends StateContainer<T & any> {

this.name = name;
this.options = options;
this.connection = new Connection(undefined, false);

this.onLeave.add(() => {
this.removeAllListeners();
});
this.onLeave.add(() => this.removeAllListeners());
}

public connect(connection: Connection) {
this.connection = connection;
public connect(endpoint: string) {
this.connection.url = endpoint;
this.connection.reconnectEnabled = false;
this.connection.onmessage = this.onMessageCallback.bind(this);
this.connection.onclose = (e) => this.onLeave.dispatch(e);
this.connection.onerror = (e) => {
console.warn(`Possible causes: room's onAuth() failed or maxClients has been reached.`);
this.onError.dispatch(e);
};
this.connection.open();
}

public leave(): void {
Expand Down
2 changes: 1 addition & 1 deletion test/room_test.ts
Expand Up @@ -18,7 +18,7 @@ describe("Room", function() {
});

it("should emit state change", function(done) {
room.onStateChange.add(function(data) {
room.onStateChange.add(function(data, previousState) {
assert.deepEqual(data.messages, []);
done();
});
Expand Down

0 comments on commit 40a4108

Please sign in to comment.