Support websocket over ssl, pass uuid in url

This commit is contained in:
Ken-Håvard Lieng 2015-12-29 22:23:07 +01:00
parent 12d6cdd4c8
commit de1a2dd343
5 changed files with 49 additions and 41 deletions

View file

@ -9,23 +9,27 @@ import handleSocket from './socket';
import { createUUID } from './util';
import Root from './containers/Root';
const socket = __DEV__ ?
new Socket(`${window.location.hostname}:1337`) :
new Socket(window.location.host);
const store = configureStore(socket);
const routes = createRoutes();
const history = createBrowserHistory();
syncReduxAndRouter(history, store);
handleSocket(socket, store);
const host = __DEV__ ? `${window.location.hostname}:1337` : window.location.host;
let uuid = localStorage.uuid;
let newUser = false;
if (!uuid) {
store.dispatch(replacePath('/connect'));
localStorage.uuid = uuid = createUUID();
uuid = createUUID();
newUser = true;
}
socket.on('connect', () => socket.send('uuid', uuid));
const socket = new Socket(host, uuid);
const store = configureStore(socket);
handleSocket(socket, store);
const history = createBrowserHistory();
syncReduxAndRouter(history, store);
if (newUser) {
store.dispatch(replacePath('/connect'));
localStorage.uuid = uuid;
}
const routes = createRoutes();
render(<Root store={store} routes={routes} history={history} />, document.getElementById('root'));

View file

@ -2,10 +2,16 @@ import EventEmitter2 from 'eventemitter2';
import Backoff from 'backo';
export default class Socket extends EventEmitter2 {
constructor(address) {
constructor(host, uuid) {
super();
this.address = address;
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
this.url = `${protocol}://${host}/ws`;
if (uuid) {
this.url += `?uuid=${uuid}`;
}
this.connectTimeout = 20000;
this.pingTimeout = 30000;
this.backoff = new Backoff({
@ -18,7 +24,7 @@ export default class Socket extends EventEmitter2 {
}
connect() {
this.ws = new WebSocket(`ws://${this.address}/ws`);
this.ws = new WebSocket(this.url);
this.timeoutConnect = setTimeout(() => {
this.ws.close();