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

File diff suppressed because one or more lines are too long

View File

@ -9,23 +9,27 @@ import handleSocket from './socket';
import { createUUID } from './util'; import { createUUID } from './util';
import Root from './containers/Root'; import Root from './containers/Root';
const socket = __DEV__ ? const host = __DEV__ ? `${window.location.hostname}:1337` : window.location.host;
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);
let uuid = localStorage.uuid; let uuid = localStorage.uuid;
let newUser = false;
if (!uuid) { if (!uuid) {
store.dispatch(replacePath('/connect')); uuid = createUUID();
localStorage.uuid = 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')); 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'; import Backoff from 'backo';
export default class Socket extends EventEmitter2 { export default class Socket extends EventEmitter2 {
constructor(address) { constructor(host, uuid) {
super(); 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.connectTimeout = 20000;
this.pingTimeout = 30000; this.pingTimeout = 30000;
this.backoff = new Backoff({ this.backoff = new Backoff({
@ -18,7 +24,7 @@ export default class Socket extends EventEmitter2 {
} }
connect() { connect() {
this.ws = new WebSocket(`ws://${this.address}/ws`); this.ws = new WebSocket(this.url);
this.timeoutConnect = setTimeout(() => { this.timeoutConnect = setTimeout(() => {
this.ws.close(); this.ws.close();

View File

@ -59,7 +59,10 @@ func upgradeWS(w http.ResponseWriter, r *http.Request) {
return return
} }
newWSHandler(conn).run() uuid := r.URL.Query().Get("uuid")
if uuid != "" {
newWSHandler(conn, uuid).run()
}
} }
func reconnect() { func reconnect() {

View File

@ -12,20 +12,18 @@ import (
) )
type wsHandler struct { type wsHandler struct {
ws *wsConn ws *wsConn
session *Session session *Session
addr string
uuid string
addr string
handlers map[string]func([]byte) handlers map[string]func([]byte)
} }
func newWSHandler(conn *websocket.Conn) *wsHandler { func newWSHandler(conn *websocket.Conn, uuid string) *wsHandler {
h := &wsHandler{ h := &wsHandler{
ws: newWSConn(conn), ws: newWSConn(conn),
addr: conn.RemoteAddr().String(), addr: conn.RemoteAddr().String(),
} }
h.init(uuid)
h.initHandlers() h.initHandlers()
return h return h
} }
@ -54,13 +52,11 @@ func (h *wsHandler) dispatchRequest(req WSRequest) {
} }
} }
func (h *wsHandler) init(b []byte) { func (h *wsHandler) init(uuid string) {
json.Unmarshal(b, &h.uuid) log.Println(h.addr, "set UUID", uuid)
log.Println(h.addr, "set UUID", h.uuid)
sessionLock.Lock() sessionLock.Lock()
if storedSession, exists := sessions[h.uuid]; exists { if storedSession, exists := sessions[uuid]; exists {
sessionLock.Unlock() sessionLock.Unlock()
h.session = storedSession h.session = storedSession
h.session.setWS(h.addr, h.ws) h.session.setWS(h.addr, h.ws)
@ -84,9 +80,9 @@ func (h *wsHandler) init(b []byte) {
} }
} else { } else {
h.session = NewSession() h.session = NewSession()
h.session.user = storage.NewUser(h.uuid) h.session.user = storage.NewUser(uuid)
sessions[h.uuid] = h.session sessions[uuid] = h.session
sessionLock.Unlock() sessionLock.Unlock()
h.session.setWS(h.addr, h.ws) h.session.setWS(h.addr, h.ws)
@ -237,7 +233,6 @@ func (h *wsHandler) search(b []byte) {
func (h *wsHandler) initHandlers() { func (h *wsHandler) initHandlers() {
h.handlers = map[string]func([]byte){ h.handlers = map[string]func([]byte){
"uuid": h.init,
"connect": h.connect, "connect": h.connect,
"join": h.join, "join": h.join,
"part": h.part, "part": h.part,