solved spectator disconnect issue
This commit is contained in:
parent
eac39ad9d1
commit
bc58ee9165
2
game.go
2
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),
|
||||
|
@ -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 {
|
||||
|
32
protocol.go
32
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)
|
||||
}
|
||||
}
|
||||
|
17
spectator.go
17
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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user