Skip to content

Commit

Permalink
support Node.js environment. closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jan 21, 2019
1 parent 129f15a commit fd842da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/colyseus.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.9.15",
"version": "0.9.16",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand Down Expand Up @@ -30,7 +30,7 @@
"@gamestdio/clock": "^1.1.0",
"@gamestdio/signals": "^1.0.0",
"@gamestdio/state-listener": "^3.1.0",
"@gamestdio/websocket": "^0.2.8",
"@gamestdio/websocket": "^0.3.0",
"fossil-delta": "^1.0.0",
"notepack.io": "^2.1.3"
},
Expand All @@ -51,6 +51,7 @@
"tslint": "^5.9.1",
"typescript": "^2.8.1",
"uglify-js": "^2.6.1",
"webpack": "^3.6.0"
"webpack": "^3.6.0",
"ws": "^6.1.2"
}
}
2 changes: 1 addition & 1 deletion src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Connection extends WebSocketClient {
}

public send(data: any): void {
if (this.ws.readyState === WebSocket.OPEN) {
if (this.ws.readyState === WebSocketClient.OPEN) {
return super.send( msgpack.encode(data) );

} else {
Expand Down
18 changes: 15 additions & 3 deletions src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
* loaded.
*/

let storage: any;

function getStorage() {
return (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: window.localStorage; // regular browser environment
if (!storage) {
storage = (typeof (cc) !== 'undefined' && cc.sys && cc.sys.localStorage)
? cc.sys.localStorage // compatibility with cocos creator
: typeof (window) !== "undefined"
? window.localStorage // regular browser environment
: { // mock localStorage for Node.js environment
cache: {},
setItem: function(key, value) { this.cache[key] = value; },
getItem: function(key) { this.cache[key]; },
};

}
return storage;
}

export function setItem(key: string, value: string) {
Expand Down

0 comments on commit fd842da

Please sign in to comment.