Join same room when giving roomID (#111)

This commit is contained in:
giongto35 2019-10-17 03:49:24 +08:00 committed by GitHub
parent 5c770186e3
commit d01521f4ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 28 deletions

View file

@ -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

23
web/js/init.js vendored
View file

@ -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);
});

26
web/js/ws.js vendored
View file

@ -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;
}