我对 Go 非常陌生,并且发现自己将套接字作为我的第一个项目。这是一个多余的问题,但我无法理解如何将 websocket 更新发送到 Go 中的特定客户端(使用 Gorilla)。
我试图解决的广泛问题是 - 使用 websockets 和 ES/Lucene 之类的搜索引擎构建预先输入。我在我的搜索引擎上维护了一堆索引,并在它周围有一个 Go 包装器。当我开始在 Go 中使用 websockets 时,我找到了几乎所有显示广播机制的示例。当我尝试深入研究并尝试根据此线程和此答案中给出的示例修改 Gorilla 的 github 存储库中给出的示例时,我似乎不明白它是如何适应的connectionsclient.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 …Run Code Online (Sandbox Code Playgroud)