github.com/alpe/etcd@v0.1.2-0.20130915230056-09f31af88aeb/web/conn.go (about)

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