diff --git a/Dockerfile b/Dockerfile index 01aed7e5..99d86a05 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,7 @@ RUN apt-get update RUN apt-get install pkg-config libvpx-dev -y RUN go get github.com/pion/webrtc RUN go get github.com/gorilla/websocket +RUN go get github.com/satori/go.uuid RUN go install github.com/giongto35/cloud-game EXPOSE 8000 diff --git a/main.go b/main.go index 45e1ea71..a08f4068 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,8 @@ var serverID = "" var oclient *Client func main() { + rand.Seed(time.Now().UTC().UnixNano()) + flag.Parse() fmt.Println("Usage: ./game [debug]") if *config.IsDebug { @@ -209,6 +211,10 @@ func ws(w http.ResponseWriter, r *http.Request) { // The server session is maintaining } + client.receive("heartbeat", func(resp WSPacket) WSPacket { + return resp + }) + client.receive("initwebrtc", func(resp WSPacket) WSPacket { log.Println("Received user SDP") localSession, err := wssession.peerconnection.StartClient(resp.Data, width, height) @@ -401,12 +407,14 @@ func removeSession(w *webrtc.WebRTC, room *Room) { } func getServerIDOfRoom(oc *Client, roomID string) string { + log.Println("Request overlord roomID") packet := oc.syncSend( WSPacket{ ID: "getRoom", Data: roomID, }, ) + log.Println("Received roomID from overlord") return packet.Data } diff --git a/static/js/ws.js b/static/js/ws.js index 43b12886..612bfbba 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -43,12 +43,7 @@ conn.onmessage = e => { //pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(d["data"])))); //conn.send(JSON.stringify({"id": "remotestart", "data": GAME_LIST[gameIdx].nes, "room_id": roomID.value, "player_index": parseInt(playerIndex.value, 10)}));inputTimer //break; - case "pong": - // TODO: Change name use one session - log("Recv pong. Start webrtc"); - //startWebRTC(); - break; - case "pingpong": + case "heartbeat": console.log("Ping: ", Date.now() - d["data"]) // TODO: Calc time break; @@ -68,7 +63,7 @@ conn.onmessage = e => { function sendPing() { // TODO: format the package with time - conn.send(JSON.stringify({"id": "pingpong", "data": Date.now().toString()})); + conn.send(JSON.stringify({"id": "heartbeat", "data": Date.now().toString()})); } function startWebRTC() { diff --git a/ws.go b/ws.go index f9d8d4e7..a8e34787 100644 --- a/ws.go +++ b/ws.go @@ -108,7 +108,7 @@ func (c *Client) heartbeat() { func (c *Client) listen() { for { - log.Println("Waiting for message") + log.Println("Waiting for message ...") _, rawMsg, err := c.conn.ReadMessage() if err != nil { log.Println("[!] read:", err) @@ -116,7 +116,7 @@ func (c *Client) listen() { } wspacket := WSPacket{} err = json.Unmarshal(rawMsg, &wspacket) - if err != nil || wspacket.ID == "heartbeat" { + if err != nil { continue }