From dd8ea60cbf8a170290e7eb0c663cd117e6b6cdd5 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 7 Apr 2019 18:10:25 +0800 Subject: [PATCH] Add Room logic --- index_ws.html | 14 ++++++++------ main.go | 31 ++++++++++++++++++++++++------- ui/gameview.go | 2 -- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/index_ws.html b/index_ws.html index dca9e629..14b0d9fc 100644 --- a/index_ws.html +++ b/index_ws.html @@ -22,7 +22,7 @@ textarea { -Room ID:
+Room ID:


@@ -99,13 +99,19 @@ window.startGame = () => { log("Got remote sdp"); pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(d["data"])))); break; + case "start": + log("Got start"); + document.getElementById('roomID').value = d["room_id"] + break; } } // webrtc pc = new RTCPeerConnection({iceServers: [{urls: 'stun:stun.l.google.com:19302'}]}) // input channel - inputChannel = pc.createDataChannel('foo') + let r = Math.random().toString(36).substring(7) + console.log("data chan name", r) + inputChannel = pc.createDataChannel(r) inputChannel.onclose = () => { log('inputChannel has closed'); } @@ -167,10 +173,6 @@ var localSessionDescription = ""; var remoteSessionDescription = ""; var conn; - - - - // Input handler keyState = { // controllers diff --git a/main.go b/main.go index 6a3134a5..5569d6e6 100644 --- a/main.go +++ b/main.go @@ -83,6 +83,26 @@ func getWeb(w http.ResponseWriter, r *http.Request) { w.Write(bs) } +func startSession(webRTC *webrtc.WebRTC, gameName string, roomID string) string { + if roomID == "" { + roomID = generateRoomID() + imageChannel := make(chan *image.RGBA, 100) + inputChannel := make(chan int, 100) + rooms[roomID] = &Room{ + imageChannel: imageChannel, + inputChannel: inputChannel, + rtcSessions: []*webrtc.WebRTC{}, + } + go fanoutScreen(imageChannel, roomID) + go startGame("games/"+gameName, imageChannel, inputChannel, webRTC) + } + + rooms[roomID].rtcSessions = append(rooms[roomID].rtcSessions, webRTC) + faninInput(rooms[roomID].inputChannel, webRTC) + + return roomID +} + func ws(w http.ResponseWriter, r *http.Request) { c, err := upgrader.Upgrade(w, r, nil) if err != nil { @@ -100,6 +120,7 @@ func ws(w http.ResponseWriter, r *http.Request) { isDone := false var gameName string + var roomID string for !isDone { mt, message, err := c.ReadMessage() @@ -121,6 +142,7 @@ func ws(w http.ResponseWriter, r *http.Request) { switch req.ID { case "ping": gameName = req.Data + roomID = req.RoomID log.Println("Ping from server with game:", gameName) res.ID = "pong" @@ -133,7 +155,6 @@ func ws(w http.ResponseWriter, r *http.Request) { res.ID = "sdp" res.Data = localSession - res.RoomID = generateRoomID() case "candidate": hi := pionRTC.ICECandidateInit{} @@ -147,10 +168,9 @@ func ws(w http.ResponseWriter, r *http.Request) { case "start": log.Println("Starting game") - imageChannel := make(chan *image.RGBA, 100) - //go screenshotLoop(imageChannel, webRTC) - go startGame("games/"+gameName, imageChannel, webRTC.InputChannel, webRTC) res.ID = "start" + res.RoomID = startSession(webRTC, gameName, roomID) + isDone = true } @@ -197,7 +217,6 @@ func postSession(w http.ResponseWriter, r *http.Request) { } roomID := postPacket.RoomID - fmt.Println("RoomID", roomID) if roomID == "" { fmt.Println("Init Room") //generate new room @@ -255,8 +274,6 @@ func fanoutScreen(imageChannel chan *image.RGBA, roomID string) { webRTC.ImageChannel <- yuv } } - // time.Sleep(10 * time.Millisecond) - // time.Sleep(time.Duration(1000 / FPS) * time.Millisecond) } } diff --git a/ui/gameview.go b/ui/gameview.go index 6d636a61..cdef94f0 100644 --- a/ui/gameview.go +++ b/ui/gameview.go @@ -24,7 +24,6 @@ type GameView struct { imageChannel chan *image.RGBA inputChannel chan int - } func NewGameView(director *Director, console *nes.Console, title, hash string, imageChannel chan *image.RGBA, inputChannel chan int) View { @@ -90,7 +89,6 @@ func (view *GameView) Update(t, dt float64) { // fps to set frame view.imageChannel <- console.Buffer() - if view.record { view.frames = append(view.frames, copyImage(console.Buffer())) }