server/spectator.go

38 lines
658 B
Go
Raw Normal View History

2013-08-19 22:23:35 -07:00
package main
import (
"code.google.com/p/go.net/websocket"
"log"
2013-08-19 22:23:35 -07:00
)
type Spectator struct {
2013-08-19 23:44:00 -07:00
ws *websocket.Conn
2013-11-13 22:24:54 -08:00
send chan Message
2014-01-29 22:12:26 -08:00
Id string
2013-08-19 22:23:35 -07:00
}
func (s *Spectator) sender() {
2014-01-29 22:12:26 -08:00
log.Printf("%s: sender launched", s.Id)
2013-08-19 22:23:35 -07:00
for things := range s.send {
2013-11-13 22:24:54 -08:00
err := websocket.JSON.Send(s.ws, things)
2013-08-19 22:23:35 -07:00
if err != nil {
break
}
}
s.ws.Close()
2014-01-29 22:12:26 -08:00
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()
2013-08-19 22:23:35 -07:00
}