Skip to content

Instantly share code, notes, and snippets.

@eikeon
Created August 29, 2013 17:26
Show Gist options
  • Save eikeon/6380948 to your computer and use it in GitHub Desktop.
Save eikeon/6380948 to your computer and use it in GitHub Desktop.
type stateServer struct {
marvin *marvin.Marvin
send chan marvin.State
}
func (s stateServer) wsHandler(ws *websocket.Conn) {
s.send = make(chan marvin.State)
s.marvin.Register(&s.send)
defer func() { s.marvin.Unregister(&s.send) }()
go func() {
for {
var msg message
if err := websocket.JSON.Receive(ws, &msg); err == nil {
if msg["message"] != nil {
req := ws.Request()
who := req.RemoteAddr
if req.TLS != nil {
for _, c := range req.TLS.PeerCertificates {
who = c.Subject.CommonName
}
}
s.marvin.Do(who, msg["message"].(string), "web")
} else {
log.Printf("ignoring: %#v\n", msg)
}
} else {
log.Println("State Websocket receive err:", err)
return
}
}
}()
for state := range s.send {
if err := websocket.JSON.Send(ws, state); err != nil {
log.Println("State Websocket send err:", err)
ws.Close()
return
}
}
ws.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment