From bc58ee9165624b5b444066539db0d877062dc93c Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 29 Jan 2014 22:12:26 -0800 Subject: [PATCH] solved spectator disconnect issue --- game.go | 2 +- player.go | 1 + protocol.go | 32 +++++++++++++++++++++++++++----- spectator.go | 17 ++++++++++++++++- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/game.go b/game.go index 18f9cbf..3457914 100644 --- a/game.go +++ b/game.go @@ -95,7 +95,7 @@ type GameMode interface { func NewGame(id string, width, height float32, obstacles, tick, maxPoints int, mode string) *game { g := &game{ id: id, - register: make(chan *player), + register: make(chan *player, maxPlayer), unregister: make(chan *player, maxPlayer), projectiles: make(map[*Projectile]bool), splosions: make(map[*Splosion]bool), diff --git a/player.go b/player.go index 234d9a2..f79a8c1 100644 --- a/player.go +++ b/player.go @@ -14,6 +14,7 @@ type player struct { } func (p *player) sender() { + log.Printf("%s: sender launched", p.Id) for things := range p.send { err := websocket.JSON.Send(p.ws, things) if err != nil { diff --git a/protocol.go b/protocol.go index 2670bb1..fa0274e 100644 --- a/protocol.go +++ b/protocol.go @@ -172,6 +172,8 @@ func addPlayer(ws *websocket.Conn) { log.Printf("game %s: unable to send player_id to player %s", gid.Id, player_id) websocket.JSON.Send(ws, NewFailure("send error")) return + } else { + log.Printf("game %s: sent player id: %s", gid.Id, player_id) } var clientid ClientID @@ -180,6 +182,8 @@ func addPlayer(ws *websocket.Conn) { log.Printf("unable to parse ClientID: gid: %s, player: %s", gid.Id, player_id) websocket.JSON.Send(ws, NewFailure("parse error")) return + } else { + log.Printf("game %s: recieved: %+v", gid.Id, clientid) } if v, msg := clientid.Valid(); !v { log.Printf("clientid is invalid: %+v", clientid) @@ -196,6 +200,8 @@ func addPlayer(ws *websocket.Conn) { log.Printf("%s %s game param parse error", gid.Id, player_id) websocket.JSON.Send(ws, NewFailure("game param parse error")) return + } else { + log.Printf("%s -> %s: sent %+v", gid.Id, player_id, gameParam) } switch clientid.Type { @@ -204,7 +210,7 @@ func addPlayer(ws *websocket.Conn) { for { log.Printf("%s Waiting for client to send conf ...", player_id) err = websocket.JSON.Receive(ws, &conf) - log.Printf("%s: conf received: %+v", player_id, conf) + log.Printf("%s: conf received: %s", player_id, conf.ID) if err != nil { log.Printf("%s %s config parse error", gid.Id, player_id) @@ -214,6 +220,7 @@ func addPlayer(ws *websocket.Conn) { // TODO: verify conf's type if conf.Valid(game.maxPoints) { + log.Printf("%s -> %s: valid client config", gid.Id, player_id) _ = websocket.JSON.Send(ws, NewHandshake(player_id, true)) break } else { @@ -225,10 +232,11 @@ func addPlayer(ws *websocket.Conn) { p := &player{ Robots: []*Robot{}, - send: make(chan Message), + send: make(chan Message, 16), ws: ws, Id: player_id, } + log.Printf("%s: made a player: %s", gid.Id, p.Id) convertedStats := map[string]Stats{} for name, stats := range conf.Stats { @@ -259,14 +267,21 @@ func addPlayer(ws *websocket.Conn) { log.Printf("error sending convertedStats to client: %s", err) websocket.JSON.Send(ws, NewFailure("protocol error: convertedStats")) return + } else { + log.Printf("%s -> %s: sent stats payload", gid.Id, p.Id) } + log.Printf("%s, %s: about to register this player", gid.Id, p.Id) game.register <- p + log.Printf("%s, %s: registered player", gid.Id, p.Id) defer func() { + log.Printf("%s, %s: about to unregister this player", gid.Id, p.Id) game.unregister <- p + log.Printf("%s, %s: unregistered player", gid.Id, p.Id) }() go p.sender() + log.Printf("%s -> %s: p.sender went", gid.Id, p.Id) p.recv() log.Printf( "%s (player): %v (robot) has been disconnected from %s (game)", @@ -276,14 +291,21 @@ func addPlayer(ws *websocket.Conn) { ) case "spectator": s := &Spectator{ - send: make(chan Message), + send: make(chan Message, 16), ws: ws, + Id: player_id, } + log.Printf("%s, %s: about to register this spectator", gid.Id, s.Id) game.sregister <- s + log.Printf("%s, %s: registered spectator", gid.Id, s.Id) defer func() { + log.Printf("%s, %s: about to unregister this spectator", gid.Id, s.Id) game.sunregister <- s + log.Printf("%s, %s: unregistered spectator", gid.Id, s.Id) }() - s.sender() - log.Printf("game %s: spectator %+v has been disconnected from this game\n", s) + go s.sender() + log.Printf("%s -> %s: s.sender went", gid.Id, s.Id) + s.recv() + log.Printf("game %s: spectator %+v has been disconnected from this game", gid.Id, s) } } diff --git a/spectator.go b/spectator.go index 7e20b5d..d109309 100644 --- a/spectator.go +++ b/spectator.go @@ -8,9 +8,11 @@ import ( type Spectator struct { ws *websocket.Conn send chan Message + Id string } func (s *Spectator) sender() { + log.Printf("%s: sender launched", s.Id) for things := range s.send { err := websocket.JSON.Send(s.ws, things) if err != nil { @@ -18,5 +20,18 @@ func (s *Spectator) sender() { } } s.ws.Close() - log.Printf("Spectator sender close") + log.Printf("%s: spectator sender close", s.Id) +} + +func (s *Spectator) recv() { + for { + var msg interface{} + err := websocket.JSON.Receive(s.ws, &msg) + if err != nil { + log.Printf("%s: problem receiving JSON from spectator: %s", s.Id, err) + break + } + } + log.Printf("%s: recv close", s.Id) + s.ws.Close() }