From 2af62fbd94b425cf0f82080d1c253a366a37389d Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sat, 20 Apr 2019 04:47:52 +0800 Subject: [PATCH] Add register room logic --- main.go | 24 ++++++++++++++++++------ overlord.go | 13 ++++++++++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 96fff91b..a5d1e2d5 100644 --- a/main.go +++ b/main.go @@ -146,6 +146,7 @@ func isRoomRunning(roomID string) bool { // startSession handles one session call func startSession(webRTC *webrtc.WebRTC, gameName string, roomID string, playerIndex int) (rRoomID string, isNewRoom bool) { + isNewRoom = false cleanSession(webRTC) // If the roomID is empty, // or the roomID doesn't have any running sessions (room was closed) @@ -162,7 +163,7 @@ func startSession(webRTC *webrtc.WebRTC, gameName string, roomID string, playerI webRTC.AttachRoomID(roomID) go startWebRTCSession(room, webRTC, playerIndex) - return roomID, false + return roomID, isNewRoom } // Session represents a session connected from the browser to the current server @@ -184,7 +185,6 @@ func ws(w http.ResponseWriter, r *http.Request) { var roomID string var playerIndex int - var oclient *Client // Create connection to overlord client := NewClient(c, webrtc.NewWebRTC()) @@ -252,10 +252,17 @@ func ws(w http.ResponseWriter, r *http.Request) { //log.Println("Ping from server with game:", gameName) //res.ID = "pong" log.Println("Starting game") + roomServerID := <-GetServerIDOfRoom(wssession.oclient, roomID) + log.Println("Server of RoomID ", roomID, " is ", roomServerID) + if roomServerID != "" && wssession.ServerID != roomServerID { + // TODO: Re -register + return + } + roomID, isNewRoom = startSession(client.peerconnection, gameName, roomID, playerIndex) if isNewRoom { - oclient.send(WSPacket{ - ID: "RegisterRoom", + wssession.oclient.send(WSPacket{ + ID: "registerRoom", Data: roomID, }) } @@ -375,12 +382,14 @@ func removeSession(w *webrtc.WebRTC, room *Room) { } } -func GetServerIDOfRoom(oc Client, roomID string) chan string { +func GetServerIDOfRoom(oc *Client, roomID string) chan string { res := make(chan string) oc.syncSend(WSPacket{ - ID: "getRoom", + ID: "getRoom", + Data: roomID, }, func(resp WSPacket) { + log.Println("GetRoom", resp.Data) res <- resp.Data }) return res @@ -427,5 +436,8 @@ func (s *Session) NewOverlordClient() { go oclient.listen() + s.oclient = oclient + // TODO: return oclient + return } diff --git a/overlord.go b/overlord.go index 8ab29130..647c9c37 100644 --- a/overlord.go +++ b/overlord.go @@ -10,7 +10,7 @@ import ( "github.com/giongto35/cloud-game/webrtc" ) -var overlordRooms = map[string]string{} +var roomToServer = map[string]string{} // servers are the map serverID to server Client var servers = map[string]Client{} @@ -46,12 +46,19 @@ func wso(w http.ResponseWriter, r *http.Request) { }) client.syncReceive("registerRoom", func(resp WSPacket) WSPacket { - log.Println("received registerRoom") - overlordRooms[resp.Data] = serverID + log.Println("Received registerRoom ", resp.Data, serverID) + roomToServer[resp.Data] = serverID return WSPacket{ ID: "registerRoom", } }) + client.syncReceive("getRoom", func(resp WSPacket) WSPacket { + return WSPacket{ + ID: "getRoom", + Data: roomToServer[resp.Data], + } + }) + client.listen() }