package main import ( "code.google.com/p/go.net/websocket" "encoding/json" "log" ) 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 { b, err := json.Marshal(things) if err != nil { break } // XXX: send the count to our bandwidth analyzer ... _, err = s.ws.Write(b) if err != nil { break } } s.ws.Close() log.Printf("%s: spectator sender close", s.Id) } func (s *Spectator) recv() { var msg interface{} buff := make([]byte, maxMessageSize) for { n, err := s.ws.Read(buff) if err != nil { log.Printf("%s: problem reading from player: %s", s.Id, err) break } // XXX: send n to our bandwidth analyzer ... log.Println(string(buff[:n])) if n == len(buff) { log.Printf("%s: read buffer overfull: %s", s.Id, string(buff)) break } err = json.Unmarshal(buff[:n], &msg) log.Println(n) if err != nil { log.Printf("%s: problem reading from player: %s", s.Id, err) break } // After the first bit of handshaking, the rest of the messages should // only be "{}" for spectators, and the following could hold true: // // if string(buff[:n]) != "{}" { // log.Printf("protocol breach!!") // break // } } log.Printf("%s: recv close", s.Id) s.ws.Close() }