Created
August 29, 2013 17:26
-
-
Save eikeon/6380948 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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