mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-23 10:07:30 +00:00
Add Room logic
This commit is contained in:
parent
b2cf0a867f
commit
dd8ea60cbf
3 changed files with 32 additions and 15 deletions
|
|
@ -22,7 +22,7 @@ textarea {
|
|||
<option value="zelda.rom">zelda.rom</option>
|
||||
</select>
|
||||
<button id="play" onclick="window.startGame()">Play</button>
|
||||
Room ID: <input type="text" name="roomID"><br>
|
||||
Room ID: <input type="text" id="roomID"><br>
|
||||
|
||||
<br/><br/>
|
||||
|
||||
|
|
@ -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
|
||||
|
|
|
|||
31
main.go
31
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue