Skip to content

Commit

Permalink
fix react-native compatibility. bump v0.7.3 #11
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Sep 30, 2017
1 parent 8e5b808 commit 6153dbc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -107,18 +107,20 @@ room.onLeave.add(function() {
## React Native compatibility

This client works with React Native. You need to install an aditional dependency
for compatibility.
for compatibility and assign `window.localStorage` to `AsyncStorage`.

```
npm install react-native-browser-polyfill
```

```
// App.js
require('react-native-browser-polyfill');
import 'react-native-browser-polyfill';
import { AsyncStorage } from 'react-native';
window.localStorage = AsyncStorage;
```

The only caveat inside React Native environment is that you can only join rooms
after the first connection open.
Another caveat is that you can only join rooms after the first connection open.

```
var client = new Colyseus.Client('ws://localhost:2657');
Expand Down
17 changes: 4 additions & 13 deletions dist/colyseus.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "colyseus.js",
"version": "0.7.2",
"version": "0.7.3",
"description": "Multiplayer Game Client for the Browser",
"keywords": [
"multiplayer",
Expand Down
10 changes: 3 additions & 7 deletions src/Client.ts
Expand Up @@ -5,11 +5,6 @@ import { Protocol } from "./Protocol";
import { Room } from "./Room";
import { Connection } from "./Connection";

// compatibility with react-native
let storage = (typeof(localStorage) === "undefined")
? require('react-native').AsyncStorage
: window.localStorage;

export class Client {
public id?: string;

Expand All @@ -24,10 +19,11 @@ export class Client {
protected rooms: {[id: string]: Room} = {};

protected hostname: string;
protected storage: Storage = window.localStorage;

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

if (!(colyseusid instanceof Promise)) {
// browser has synchronous return
Expand Down Expand Up @@ -71,7 +67,7 @@ export class Client {
let code = message[0];

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

this.id = message[1];
this.onOpen.dispatch();
Expand Down

0 comments on commit 6153dbc

Please sign in to comment.