diff --git a/pkg/overlord/handlers.go b/pkg/overlord/handlers.go index d83905f5..b055c5eb 100644 --- a/pkg/overlord/handlers.go +++ b/pkg/overlord/handlers.go @@ -129,10 +129,26 @@ func (o *Server) WS(w http.ResponseWriter, r *http.Request) { client := NewBrowserClient(c) go client.Listen() - // Get best server for frontend to connect to - workerClient, err := o.getBestWorkerClient(client) - if err != nil { - return + var workerClient *WorkerClient + + // get roomID if it is embeded in request. Server will pair the frontend with the server running the room. It only happens when we are trying to access a running room over share link. + // TODO: Update link to the wiki + roomID := r.URL.Query().Get("room_id") + if roomID != "" { + log.Printf("Detected roomID %v from URL", roomID) + if workerID, ok := o.roomToServer[roomID]; ok { + workerClient = o.workerClients[workerID] + log.Printf("Found running server with id=%v client=%v", workerID, workerClient) + } + } + + // If there is no existing server to connect to, we find the best possible worker for the frontend + if workerClient == nil { + // Get best server for frontend to connect to + workerClient, err = o.getBestWorkerClient(client) + if err != nil { + return + } } // SessionID will be the unique per frontend connection diff --git a/web/js/init.js b/web/js/init.js index ac366ae4..e80958d9 100644 --- a/web/js/init.js +++ b/web/js/init.js @@ -35,32 +35,9 @@ $(window).on("resize", fixScreenLayout); $(window).on("orientationchange", fixScreenLayout); -function parseURLForRoom() { - var queryDict = {} - location.search.substr(1).split("&").forEach(function(item) { - queryDict[item.split("=")[0]] = item.split("=")[1] - }); - if (typeof queryDict["id"] === "string") { - return decodeURI(queryDict["id"]); - } - return null; -} - $(document).ready(function () { fixScreenLayout(); - - // localStorage first - //roomID = loadRoomID(); - roomID = ""; - - // Shared URL second - var rid = parseURLForRoom(); - if (rid !== null) { - roomID = rid; - } - // if from URL -> start game immediately! - $("#room-txt").val(roomID); }); diff --git a/web/js/ws.js b/web/js/ws.js index 734ef954..5ebd93a5 100644 --- a/web/js/ws.js +++ b/web/js/ws.js @@ -2,7 +2,19 @@ var curPacketID = ""; var curSessionID = ""; // web socket -conn = new WebSocket(`ws://${location.host}/ws`); +// localStorage first +//roomID = loadRoomID(); +roomID = ""; + +// Shared URL second +var rid = parseURLForRoom(); +if (rid !== null) { + roomID = rid; +} + +// if from URL -> start game immediately! +console.log(`ws://${location.host}/ws?room_id=${roomID}`) +conn = new WebSocket(`ws://${location.host}/ws?room_id=${roomID}`); // Clear old roomID conn.onopen = () => { @@ -288,3 +300,15 @@ function startGame() { function isMobileDevice() { return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1); }; + +function parseURLForRoom() { + var queryDict = {} + location.search.substr(1).split("&").forEach(function(item) { + queryDict[item.split("=")[0]] = item.split("=")[1] + }); + if (typeof queryDict["id"] === "string") { + return decodeURI(queryDict["id"]); + } + return null; +} +