From 2be2e4d4129f0bb0081f5153d44b59b974fed6df Mon Sep 17 00:00:00 2001 From: giongto35 Date: Thu, 2 May 2019 04:16:45 +0800 Subject: [PATCH] Fix register room bug --- cmd/main_test.go | 2 ++ handler/browser.go | 5 ++++- handler/handlers.go | 2 +- handler/overlord.go | 7 +------ 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/main_test.go b/cmd/main_test.go index 3e007da3..c01f3ba3 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -336,3 +336,5 @@ func initClient2(t *testing.T, host string, remoteRoomID string) (conn *websocke // If receive roomID, the server is running correctly return ws, roomID } + +// TODO Add test reconnect room diff --git a/handler/browser.go b/handler/browser.go index 7b7817c5..a806f8a7 100644 --- a/handler/browser.go +++ b/handler/browser.go @@ -93,7 +93,10 @@ func (s *Session) RegisterBrowserClient() { // Create new room // TODO: check if roomID is in the current server - room := s.handler.createNewRoom(s.GameName, s.RoomID, s.PlayerIndex) + room := s.handler.getRoom(s.RoomID) + if room == nil { + room = s.handler.createNewRoom(s.GameName, s.RoomID, s.PlayerIndex) + } // Attach peerconnection to room room.addConnectionToRoom(s.peerconnection, s.PlayerIndex) s.RoomID = room.ID diff --git a/handler/handlers.go b/handler/handlers.go index ed50effe..7e31c0bd 100644 --- a/handler/handlers.go +++ b/handler/handlers.go @@ -123,7 +123,7 @@ func (h *Handler) createNewRoom(gameName string, roomID string, playerIndex int) if roomID == "" || !h.isRoomRunning(roomID) { room := NewRoom(roomID, gameName) // TODO: Might have race condition - h.rooms[roomID] = room + h.rooms[room.ID] = room return room } diff --git a/handler/overlord.go b/handler/overlord.go index 02d0687f..3c7fd95a 100644 --- a/handler/overlord.go +++ b/handler/overlord.go @@ -83,17 +83,12 @@ func (s *Session) RegisterOverlordClient() { // Request room from Server if roomID is existed on the server room := s.handler.getRoom(s.RoomID) if room == nil { - log.Println("Room not found", s.RoomID) + log.Println("Room not found ", s.RoomID) return cws.EmptyPacket } room.addConnectionToRoom(peerconnection, s.PlayerIndex) //roomID, isNewRoom := startSession(peerconnection, resp.Data, resp.RoomID, resp.PlayerIndex) log.Println("Done, sending back") - // Bridge always access to old room - // TODO: log warn - if room != nil { - log.Fatal("Bridge should not spawn new room") - } req.ID = "start" req.RoomID = room.ID