mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-19 01:24:26 +00:00
Close input chan after quit game
This commit is contained in:
parent
61fe8923fa
commit
6b3545efc0
3 changed files with 34 additions and 29 deletions
3
static/js/controller.js
vendored
3
static/js/controller.js
vendored
|
|
@ -247,7 +247,8 @@ function doButtonUp(name) {
|
|||
// TODO: Stop game
|
||||
conn.send(JSON.stringify({ "id": "quit", "data": "", "room_id": roomID }));
|
||||
|
||||
$("#room-txt").val("");
|
||||
roomID = ""
|
||||
$("#room-txt").val(roomID);
|
||||
popup("Quit!");
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,7 +96,18 @@ func (h *Handler) detachPeerConn(pc *webrtc.WebRTC) {
|
|||
if room == nil {
|
||||
return
|
||||
}
|
||||
room.CleanSession(pc)
|
||||
|
||||
// If room has no sessions, close room
|
||||
if !room.EmptySessions() {
|
||||
room.RemoveSession(pc)
|
||||
if room.EmptySessions() {
|
||||
log.Println("No session in room")
|
||||
room.Close()
|
||||
// Signal end of input Channel
|
||||
log.Println("Signal input chan")
|
||||
pc.InputChannel <- -1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getRoom returns room from roomID
|
||||
|
|
|
|||
|
|
@ -120,33 +120,29 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC, playerIndex int
|
|||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for input := range peerconnection.InputChannel {
|
||||
if peerconnection.Done || !peerconnection.IsConnected() || !r.IsRunning {
|
||||
return
|
||||
}
|
||||
for input := range peerconnection.InputChannel {
|
||||
// NOTE: when room is no longer running. InputChannel needs to have extra event to go inside the loop
|
||||
if peerconnection.Done || !peerconnection.IsConnected() || !r.IsRunning {
|
||||
break
|
||||
}
|
||||
|
||||
if peerconnection.IsConnected() {
|
||||
// the first 8 bits belong to player 1
|
||||
// the next 8 belongs to player 2 ...
|
||||
// We standardize and put it to inputChannel (16 bits)
|
||||
input = input << ((uint(playerIndex) - 1) * emulator.NumKeys)
|
||||
select {
|
||||
case r.inputChannel <- input:
|
||||
default:
|
||||
}
|
||||
if peerconnection.IsConnected() {
|
||||
// the first 8 bits belong to player 1
|
||||
// the next 8 belongs to player 2 ...
|
||||
// We standardize and put it to inputChannel (16 bits)
|
||||
input = input << ((uint(playerIndex) - 1) * emulator.NumKeys)
|
||||
select {
|
||||
case r.inputChannel <- input:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
log.Println("Peerconn done")
|
||||
}
|
||||
|
||||
func (r *Room) CleanSession(peerconnection *webrtc.WebRTC) {
|
||||
r.removeSession(peerconnection)
|
||||
}
|
||||
|
||||
func (r *Room) removeSession(w *webrtc.WebRTC) {
|
||||
// RemoveSession removes a peerconnection from room and return true if there is no more room
|
||||
func (r *Room) RemoveSession(w *webrtc.WebRTC) {
|
||||
log.Println("Cleaning session: ", w.ID)
|
||||
// TODO: get list of r.rtcSessions in lock
|
||||
for i, s := range r.rtcSessions {
|
||||
|
|
@ -154,13 +150,6 @@ func (r *Room) removeSession(w *webrtc.WebRTC) {
|
|||
if s.ID == w.ID {
|
||||
r.rtcSessions = append(r.rtcSessions[:i], r.rtcSessions[i+1:]...)
|
||||
log.Println("Removed session ", s.ID, " from room: ", r.ID)
|
||||
|
||||
// If room has no sessions, close room
|
||||
// Note: this logic cannot be brought outside of forloop because we only close room if room had at least one session
|
||||
if len(r.rtcSessions) == 0 {
|
||||
log.Println("No session in room")
|
||||
r.Close()
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
@ -236,6 +225,10 @@ func (r *Room) LoadGame() error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (r *Room) EmptySessions() bool {
|
||||
return len(r.rtcSessions) == 0
|
||||
}
|
||||
|
||||
func (r *Room) IsRunningSessions() bool {
|
||||
// If there is running session
|
||||
for _, s := range r.rtcSessions {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue