From 152e3123cdf5ff7415b1caaafccb61b22112ae8f Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 14 Apr 2019 22:37:57 +0800 Subject: [PATCH] Remove emulator when no running session --- main.go | 9 ++++++++- static/js/ws.js | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 5b4e5846..2e91ea17 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ type Room struct { closedChannel chan bool rtcSessions []*webrtc.WebRTC + //sessionsLock sync.Mutex director *ui.Director } @@ -135,13 +136,18 @@ func isRoomRunning(roomID string) bool { // startSession cleans all of the dependencies of old game to current webRTC session func cleanSession(webrtc *webrtc.WebRTC) { roomID := webrtc.RoomID - if !isRoomRunning(roomID) { + // If no roomID is registered + if _, ok := rooms[roomID]; !ok { return } + // We search for session in sessions list and we also remove closed session for id, session := range rooms[roomID].rtcSessions { if session == webrtc { rooms[roomID].rtcSessions = append(rooms[roomID].rtcSessions[:id], rooms[roomID].rtcSessions[id+1:]...) + if !isRoomRunning(roomID) { + rooms[roomID].closedChannel <- true + } break } } @@ -159,6 +165,7 @@ func startSession(webRTC *webrtc.WebRTC, gameName string, roomID string, playerI // TODO: Might have race condition rooms[roomID].rtcSessions = append(rooms[roomID].rtcSessions, webRTC) + webRTC.AttachRoomID(roomID) faninInput(rooms[roomID].inputChannel, webRTC, playerIndex) diff --git a/static/js/ws.js b/static/js/ws.js index 8e8d6053..afcf04c3 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -59,7 +59,7 @@ function startWebRTC() { pc = new RTCPeerConnection({iceServers: [{urls: 'stun:stun.l.google.com:19302'}]}) // input channel - inputChannel = pc.createDataChannel('foo') + inputChannel = pc.createDataChannel('foo', {ordered: false}) inputChannel.onclose = () => { log('inputChannel has closed'); }