Skip to content

Commit

Permalink
refactoring localStorage usage. integrate new 'allowReconnection' fea…
Browse files Browse the repository at this point in the history
…ture from server. colyseus/colyseus#147
  • Loading branch information
endel committed May 14, 2018
1 parent 5de9017 commit 9ce82b1
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dist/colyseus.js

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"start": "npm run build && node --harmony ./examples/server.js",
"test": "mocha test/*.ts --require ts-node/register",
"build": "webpack --env.production",
"watch": "tsc -w",
Expand Down Expand Up @@ -43,15 +42,15 @@
"@types/msgpack-lite": "^0.1.1",
"benchmark": "^2.1.4",
"chai": "^3.5.0",
"mocha": "^3.1.0",
"mocha": "^5.1.1",
"msgpack-lite": "^0.1.20",
"nodemon": "^1.7.1",
"node-localstorage": "^1.3.1",
"nodemon": "^1.17.4",
"ts-loader": "^2.3.7",
"ts-node": "^3.3.0",
"ts-node": "^6.0.3",
"tslint": "^5.9.1",
"typescript": "^2.8.1",
"uglify-js": "^2.6.1",
"watchify": "^3.6.1",
"webpack": "^3.6.0"
}
}
31 changes: 11 additions & 20 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as msgpack from 'notepack.io';

import { Connection } from './Connection';
import { Protocol } from './Protocol';
import { Room, RoomAvailable } from './Room';
import { RECONNECTION_KEY, Room, RoomAvailable } from './Room';
import { getItem, setItem } from './Storage';

export class Client {
public id?: string;
Expand All @@ -21,27 +22,11 @@ export class Client {
protected requestId = 0;

protected hostname: string;
protected storage: Storage = (typeof(cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: window.localStorage; // regular browser environment

protected roomsAvailableRequests: {[requestId: number]: (value?: RoomAvailable[]) => void} = {};

constructor(url: string) {
this.hostname = url;
const colyseusid: any = this.storage.getItem('colyseusid');

if (
typeof(Promise) === 'undefined' || // old browsers
!(colyseusid instanceof Promise)
) {
// browser has synchronous return
this.connect(colyseusid);

} else {
// react-native is asynchronous
colyseusid.then((id) => this.connect(id));
}
getItem('colyseusid', (colyseusid) => this.connect(colyseusid));
}

public join<T>(roomName: string, options: any = {}): Room<T> {
Expand All @@ -57,7 +42,13 @@ export class Client {

this.connectingRooms[ options.requestId ] = room;

this.connection.send([Protocol.JOIN_ROOM, roomName, options]);
getItem(RECONNECTION_KEY, (reconnectingSessionId) => {
if (reconnectingSessionId) {
options.sessionId = reconnectingSessionId;
}

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

return room;
}
Expand Down Expand Up @@ -123,7 +114,7 @@ export class Client {
const code = message[0];

if (code === Protocol.USER_ID) {
this.storage.setItem('colyseusid', message[1]);
setItem('colyseusid', message[1]);

this.id = message[1];
this.onOpen.dispatch();
Expand Down
17 changes: 16 additions & 1 deletion src/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as msgpack from 'notepack.io';
import { Client } from './Client';
import { Connection } from './Connection';
import { Protocol } from './Protocol';
import { setItem } from './Storage';

export interface RoomAvailable {
roomId: string;
Expand All @@ -16,6 +17,8 @@ export interface RoomAvailable {
metadata?: any;
}

export const RECONNECTION_KEY = 'reconnection';

export class Room<T= any> extends StateContainer<T & any> {
public id: string;
public sessionId: string;
Expand All @@ -34,6 +37,7 @@ export class Room<T= any> extends StateContainer<T & any> {
public onLeave: Signal = new Signal();

public connection: Connection;
protected allowReconnection: boolean;
private _previousState: any;

constructor(name: string, options?: any) {
Expand All @@ -43,7 +47,10 @@ export class Room<T= any> extends StateContainer<T & any> {
this.name = name;
this.options = options;

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

public connect(connection: Connection) {
Expand Down Expand Up @@ -85,6 +92,8 @@ export class Room<T= any> extends StateContainer<T & any> {

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

} else if (code === Protocol.JOIN_ERROR) {
Expand All @@ -109,6 +118,12 @@ export class Room<T= any> extends StateContainer<T & any> {
}
}

protected refreshAutoReconnection() {
if (this.allowReconnection) {
setItem(RECONNECTION_KEY, this.sessionId);
}
}

protected setState( encodedState: Buffer, remoteCurrentTime?: number, remoteElapsedTime?: number ): void {
const state = msgpack.decode(encodedState);
this.set(state);
Expand Down
23 changes: 23 additions & 0 deletions src/Storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const storage: Storage = (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: window.localStorage; // regular browser environment

export function setItem(key: string, value: string) {
storage.setItem(key, value);
}

export function getItem(key: string, callback: Function) {
const value: any = storage.getItem('colyseusid');

if (
typeof (Promise) === 'undefined' || // old browsers
!(value instanceof Promise)
) {
// browser has synchronous return
callback(value);

} else {
// react-native is asynchronous
value.then((id) => callback(id));
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./legacy";
import './legacy';

export { Client } from './Client';
export { Protocol } from './Protocol';
Expand Down
8 changes: 4 additions & 4 deletions src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/*
* Support Android 4.4.x
*/
if (!ArrayBuffer['isView']) {
ArrayBuffer.isView = function (a: any): a is ArrayBufferView {
return a !== null && typeof (a) === "object" && a['buffer'] instanceof ArrayBuffer;
if (!ArrayBuffer.isView) {
ArrayBuffer.isView = (a: any): a is ArrayBufferView => {
return a !== null && typeof (a) === 'object' && a.buffer instanceof ArrayBuffer;
};
}
}
3 changes: 3 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import * as localStorage from "node-localstorage";

(<any>global).WebSocket = {};
(<any>global).window = { localStorage: localStorage };

0 comments on commit 9ce82b1

Please sign in to comment.