示例#1
0
/*
Connections support one concurrent reader and one concurrent writer.
Applications are responsible for ensuring that no more than one goroutine calls
the write methods (NextWriter, SetWriteDeadline, WriteMessage, WriteJSON)
concurrently and that no more than one goroutine calls the read methods
(NextReader, SetReadDeadline, ReadMessage, ReadJSON, SetPongHandler,
SetPingHandler) concurrently.
 -- http://www.gorillatoolkit.org/pkg/websocket
*/
func generateConnWriter(conn *websocket.Conn) chan<- interface{} {
	c := make(chan interface{})
	go func() {
		for {
			j := <-c
			err := conn.WriteJSON(j)
			if err != nil {
				fmt.Println("Error writing JSON:", err)
				return
			}
		}
	}()
	return c
}