在 Go 中向特定客户端发送 Websocket 消息(使用 Gorilla)

Sending a Websocket message to a specific client in Go (using Gorilla)

我是 Go 的新手,发现自己使用套接字作为我的第一个项目。这是一个多余的问题,但我不明白如何 向 Go 中的特定客户端发送 websocket 更新(使用 Gorilla)。

我试图解决的广泛问题是 - 使用 websockets 和像 ES/Lucene 这样的搜索引擎构建一个 typeahead。我在我的搜索引擎上维护了一堆索引,并且围绕它有一个 Go 包装器。当我开始在 Go 中使用 websockets 时,我发现几乎所有显示广播机制的示例。当我试图深入研究并尝试修改 Gorilla 的 github repo based on the examples given in this thread and in this answer 中给出的示例时,我似乎不理解 connections 以及它如何适合 client.go

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

服务器如何唯一标识Client

我使用了 Gorilla 的 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
}

请注意 - 我将在 Client 结构中添加一个 Id 字段。我该如何从这里开始?

聊天示例展示了如何实现广播。如果不需要广播,聊天示例不是应用程序的良好起点。

要向特定的 websocket 连接发送消息,只需使用 NextWriter or WriteMessage 写入连接即可。这些方法不支持并发编写器,因此您可能需要使用互斥锁或协程来确保单个编写器。

查找特定 *websocket.Connection 的简单方法是将 *websocket.Connection 传递给需要它的代码。如果应用程序需要将其他状态与连接相关联,则定义一个类型来保存该状态并传递指向该状态的指针:

type Client struct {
    conn *websocket.Conn
    mu sync.Mutex
    ...
}

可以修改Hub发送消息到特定的连接,但如果不需要广播,这是一个迂回的路径。方法如下:

向客户端添加 ID 字段:

 ID idType // replace idType with int, string, or whatever you want to use

将 Gorilla 中心字段从 connections map[*connection]bool 更改为 connections map[idType]*connection

定义包含消息数据和目标客户端 ID 的消息类型:

type message struct {
   ID idtype
   data []byte
}

将中心广播字段替换为:

   send chan message

将循环的中心更改为:

for {
    select {
    case client := <-h.register:
        h.clients[client.ID] = client
    case client := <-h.unregister:
        if _, ok := h.clients[client.ID]; ok {
            delete(h.clients, client.ID)
            close(client.send)
        }
    case message := <-h.send:
        if client, ok := h.clients[message.ID]; ok {
            select {
            case client.send <- message.data:
            default:
                close(client.send)
                delete(h.connections, client)
            }
        }
    }

通过创建具有适当 ID 的 message 向特定客户端发送消息:

   hub.send <- message{ID: targetID, data: data}