mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-13 05:40:20 +01:00
send empty players array if no player is online any more.
This commit is contained in:
parent
f533ed71e7
commit
c87ce03dbe
|
@ -24,10 +24,14 @@ type connection struct {
|
|||
send chan []byte
|
||||
}
|
||||
|
||||
type msg struct {
|
||||
Tiles []xz `json:"tiles,omitempty"`
|
||||
Pls []*player `json:"players,omitempty"`
|
||||
}
|
||||
type (
|
||||
tilesMsg struct {
|
||||
Tiles []xz `json:"tiles,omitempty"`
|
||||
}
|
||||
plsMsg struct {
|
||||
Pls []*player `json:"players,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
func newWebsocketForwarder() *websocketForwarder {
|
||||
upgrader := &websocket.Upgrader{
|
||||
|
@ -63,7 +67,7 @@ func (wsf *websocketForwarder) unregister(c *connection) {
|
|||
}
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) send(m *msg) {
|
||||
func (wsf *websocketForwarder) send(m interface{}) {
|
||||
wsf.funcs <- func(wsf *websocketForwarder) {
|
||||
if len(wsf.connections) == 0 {
|
||||
return
|
||||
|
@ -79,6 +83,8 @@ func (wsf *websocketForwarder) send(m *msg) {
|
|||
|
||||
data := buf.Bytes()
|
||||
|
||||
log.Printf("sending: %s\n", string(data))
|
||||
|
||||
for c := range wsf.connections {
|
||||
select {
|
||||
case c.send <- data:
|
||||
|
@ -91,11 +97,11 @@ func (wsf *websocketForwarder) send(m *msg) {
|
|||
}
|
||||
|
||||
func (wsf *websocketForwarder) BaseTilesUpdated(changes []xz) {
|
||||
wsf.send(&msg{Tiles: changes})
|
||||
wsf.send(&tilesMsg{Tiles: changes})
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) BroadcastPlayers(pls []*player) {
|
||||
wsf.send(&msg{Pls: pls})
|
||||
wsf.send(&plsMsg{Pls: pls})
|
||||
}
|
||||
|
||||
func (wsf *websocketForwarder) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -160,6 +160,9 @@ func (ps *players) run() {
|
|||
}
|
||||
ps.mu.Unlock()
|
||||
if change && ps.wsf != nil {
|
||||
if pls == nil {
|
||||
pls = []*player{}
|
||||
}
|
||||
// TODO: Throttle this!
|
||||
ps.wsf.BroadcastPlayers(pls)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user