Bha*_*nth 9 go websocket gorilla

我对 Go 非常陌生,并且发现自己将套接字作为我的第一个项目。这是一个多余的问题,但我无法理解如何将 websocket 更新发送到 Go 中的特定客户端(使用 Gorilla)。

connectionsclient.go

理想情况下,我希望看到这种工作的方式是 -

  • 建立客户端和服务器之间的套接字连接
  • 客户端通过套接字发送输入后,服务器获取它并放入通道(Go 通道)
  • 索引包装器检查这个通道,一旦有东西要获取,索引就会被检索并写回套接字
Client

我使用了 Gorilla's Github repo上给出的例子

hub.go
type Hub struct {
    // Registered clients.
    clients map[*Client]bool

    // Inbound messages from the clients.
    broadcast chan []byte

    // Register requests from the clients.
    register chan *Client

    // Unregister requests from clients.
    unregister chan *Client

    connections map[string]*connection
}

func newHub() *Hub {
    return &Hub{
        broadcast:  make(chan []byte),
        register:   make(chan *Client),
        unregister: make(chan *Client),
        clients:    make(map[*Client]bool),
        connection: make(map[*Client]bool), // is this alright?
    }
}

func (h *Hub) run() {
    for {
        select {
        case client := <-h.register:
            h.clients[client] = true
        case client := <-h.unregister:
            if _, ok := h.clients[client]; ok {
                delete(h.clients, client)
                close(client.send)
            }
        case message := <-h.broadcast:
            for client := range h.connections {
                select {
                case client.send <- message:
                default:
                    close(client.send)
                    delete(h.connections, client)
                }
            }
        }
    }
}
client.go
type Client struct {
    // unique ID for each client
    // id string

    // Hub object
    hub *Hub

    // The websocket connection.
    conn *websocket.Conn

    // Buffered channel of outbound messages.
    send chan []byte

    // connection --> (what should the connection property be?)
    connection string
}
IdClient