diff options
Diffstat (limited to 'app/lib')
| -rw-r--r-- | app/lib/backend-redux-actions.js | 5 | ||||
| -rw-r--r-- | app/lib/backend.js | 25 |
2 files changed, 29 insertions, 1 deletions
diff --git a/app/lib/backend-redux-actions.js b/app/lib/backend-redux-actions.js index 3fff1e1127..89690da2fe 100644 --- a/app/lib/backend-redux-actions.js +++ b/app/lib/backend-redux-actions.js @@ -8,6 +8,10 @@ export default function mapBackendEventsToReduxActions(backend, store) { store.dispatch(connectActions.connectionChange({ clientIp })); }; + const onUpdateLocation = (data) => { + store.dispatch(userActions.loginChange(data)); + }; + const onConnecting = (serverAddress) => { store.dispatch(connectActions.connectionChange({ status: ConnectionState.connecting, @@ -52,6 +56,7 @@ export default function mapBackendEventsToReduxActions(backend, store) { }; backend.on(Backend.EventType.updatedIp, onUpdateIp); + backend.on(Backend.EventType.updatedLocation, onUpdateLocation); backend.on(Backend.EventType.connecting, onConnecting); backend.on(Backend.EventType.connect, onConnect); backend.on(Backend.EventType.disconnect, onDisconnect); diff --git a/app/lib/backend.js b/app/lib/backend.js index 158828ab62..cf0c4258ae 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -4,7 +4,7 @@ import { EventEmitter } from 'events'; import { servers } from '../config'; import { ConnectionState as ReduxConnectionState } from '../enums'; -const EventType = Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout', 'updatedIp'); +const EventType = Enum('connect', 'connecting', 'disconnect', 'login', 'logging', 'logout', 'updatedIp', 'updatedLocation'); const ConnectionState = Enum('disconnected', 'connecting', 'connected'); /** @@ -204,11 +204,34 @@ export default class Backend extends EventEmitter { // @TODO: Add disconnect call } + _fetchLocation() { + return fetch('https://freegeoip.net/json/').then((res) => { + return res.json(); + }); + } + refreshIp() { + if(this._connStatus === ConnectionState.disconnected) { + this._fetchLocation().then((res) => { + console.log('Got location data: ', res); + const data = { + location: [ res.latitude, res.longitude ], // lat, lng + city: res.city, + country: res.country_name + }; + this.emit(EventType.updatedLocation, data); + this.emit(EventType.updatedIp, res.ip); + }).catch((error) => { + console.log('Got error: ', error); + }); + return; + } + let ip = []; for(let i = 0; i < 4; i++) { ip.push(parseInt(Math.random() * 253 + 1)); } + this.emit(EventType.updatedIp, ip.join('.')); } } |
