conn.go 530 B

123456789101112131415161718192021222324252627282930
  1. package web
  2. import (
  3. "code.google.com/p/go.net/websocket"
  4. )
  5. type connection struct {
  6. // The websocket connection.
  7. ws *websocket.Conn
  8. // Buffered channel of outbound messages.
  9. send chan string
  10. }
  11. func (c *connection) writer() {
  12. for message := range c.send {
  13. err := websocket.Message.Send(c.ws, message)
  14. if err != nil {
  15. break
  16. }
  17. }
  18. c.ws.Close()
  19. }
  20. func wsHandler(ws *websocket.Conn) {
  21. c := &connection{send: make(chan string, 256), ws: ws}
  22. h.register <- c
  23. defer func() { h.unregister <- c }()
  24. c.writer()
  25. }