You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

39 lines
672 B

package main
type game struct {
players map[*player]bool
broadcast chan *player
register chan *player
unregister chan *player
}
var g = game{
broadcast: make(chan *player),
register: make(chan *player),
unregister: make(chan *player),
players: make(map[*player]bool),
}
func (g *game) run() {
for {
select {
case c := <-g.register:
g.players[c] = true
case c := <-g.unregister:
delete(g.players, c)
close(c.send)
case p := <-g.broadcast:
for c := range g.players {
if p != c {
select {
case c.send <- p:
default:
delete(g.players, c)
close(c.send)
go c.ws.Close()
}
}
}
}
}
}