您必须使用连接池将消息广播到所有连接。您可以将其用作教程/示例http://gary.burd.info/go-websocket-
chat
简化:
连接池是已注册连接的集合。见
hub.connections:
type connection struct { // The websocket connection. ws *websocket.Conn // Buffered channel of outbound messages. send chan []byte // The hub. h *hub}type hub struct { // Registered connections. That's a connection pool connections map[*connection]bool ...}为了广播所有客户端的消息,我们在连接池上进行如下迭代:
case m := <-h.broadcast: for c := range h.connections { select { case c.send <- m: default: delete(h.connections, c) close(c.send) } } }h.broadcast在该示例中,是一个包含我们需要广播的消息的频道。
我们用
default一节的
select语句具有完全或阻止发送通道删除连接。



