From 6153dbc1986bca991bceabfb943c4c8be1407ddd Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Sat, 30 Sep 2017 17:23:44 -0300 Subject: [PATCH] fix react-native compatibility. bump v0.7.3 #11 --- README.md | 10 ++++++---- dist/colyseus.js | 17 ++++------------- package.json | 2 +- src/Client.ts | 10 +++------- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4e15472..64d72bc 100644 --- a/README.md +++ b/README.md @@ -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'); diff --git a/dist/colyseus.js b/dist/colyseus.js index 3df576e..d143e82 100644 --- a/dist/colyseus.js +++ b/dist/colyseus.js @@ -4307,10 +4307,6 @@ var signals_js_1 = __webpack_require__(22); var Protocol_1 = __webpack_require__(12); var Room_1 = __webpack_require__(26); var Connection_1 = __webpack_require__(58); -// compatibility with react-native -var storage = (typeof (localStorage) === "undefined") - ? __webpack_require__(61).AsyncStorage - : window.localStorage; var Client = /** @class */ (function () { function Client(url) { var _this = this; @@ -4320,8 +4316,9 @@ var Client = /** @class */ (function () { this.onClose = new signals_js_1.Signal(); this.onError = new signals_js_1.Signal(); this.rooms = {}; + this.storage = window.localStorage; this.hostname = url; - var colyseusid = storage.getItem('colyseusid'); + var colyseusid = this.storage.getItem('colyseusid'); if (!(colyseusid instanceof Promise)) { // browser has synchronous return this.createConnection(colyseusid); @@ -4359,7 +4356,7 @@ var Client = /** @class */ (function () { var message = msgpack.decode(new Uint8Array(event.data)); var code = message[0]; if (code == Protocol_1.Protocol.USER_ID) { - storage.setItem('colyseusid', message[1]); + this.storage.setItem('colyseusid', message[1]); this.id = message[1]; this.onOpen.dispatch(); } @@ -7103,12 +7100,6 @@ this.open(true);}/** "use strict"; Object.defineProperty(exports,"__esModule",{value:true});exports.createBackoff=createBackoff;var backoff={exponential:function exponential(attempt,delay){return Math.floor(Math.random()*Math.pow(2,attempt)*delay);},fibonacci:function fibonacci(attempt,delay){var current=1;if(attempt>current){var prev=1,current=2;for(var index=2;index