Support websocket over ssl, pass uuid in url
This commit is contained in:
parent
12d6cdd4c8
commit
de1a2dd343
File diff suppressed because one or more lines are too long
@ -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'));
|
||||
|
@ -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();
|
||||
|
@ -59,7 +59,10 @@ func upgradeWS(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
newWSHandler(conn).run()
|
||||
uuid := r.URL.Query().Get("uuid")
|
||||
if uuid != "" {
|
||||
newWSHandler(conn, uuid).run()
|
||||
}
|
||||
}
|
||||
|
||||
func reconnect() {
|
||||
|
@ -12,20 +12,18 @@ import (
|
||||
)
|
||||
|
||||
type wsHandler struct {
|
||||
ws *wsConn
|
||||
session *Session
|
||||
|
||||
uuid string
|
||||
addr string
|
||||
|
||||
ws *wsConn
|
||||
session *Session
|
||||
addr string
|
||||
handlers map[string]func([]byte)
|
||||
}
|
||||
|
||||
func newWSHandler(conn *websocket.Conn) *wsHandler {
|
||||
func newWSHandler(conn *websocket.Conn, uuid string) *wsHandler {
|
||||
h := &wsHandler{
|
||||
ws: newWSConn(conn),
|
||||
addr: conn.RemoteAddr().String(),
|
||||
}
|
||||
h.init(uuid)
|
||||
h.initHandlers()
|
||||
return h
|
||||
}
|
||||
@ -54,13 +52,11 @@ func (h *wsHandler) dispatchRequest(req WSRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *wsHandler) init(b []byte) {
|
||||
json.Unmarshal(b, &h.uuid)
|
||||
|
||||
log.Println(h.addr, "set UUID", h.uuid)
|
||||
func (h *wsHandler) init(uuid string) {
|
||||
log.Println(h.addr, "set UUID", uuid)
|
||||
|
||||
sessionLock.Lock()
|
||||
if storedSession, exists := sessions[h.uuid]; exists {
|
||||
if storedSession, exists := sessions[uuid]; exists {
|
||||
sessionLock.Unlock()
|
||||
h.session = storedSession
|
||||
h.session.setWS(h.addr, h.ws)
|
||||
@ -84,9 +80,9 @@ func (h *wsHandler) init(b []byte) {
|
||||
}
|
||||
} else {
|
||||
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()
|
||||
|
||||
h.session.setWS(h.addr, h.ws)
|
||||
@ -237,7 +233,6 @@ func (h *wsHandler) search(b []byte) {
|
||||
|
||||
func (h *wsHandler) initHandlers() {
|
||||
h.handlers = map[string]func([]byte){
|
||||
"uuid": h.init,
|
||||
"connect": h.connect,
|
||||
"join": h.join,
|
||||
"part": h.part,
|
||||
|
Loading…
Reference in New Issue
Block a user