rest - 戈朗 : Broadcast messages to websocket when a particular POST JSON is received

标签 rest post go websocket broadcast

我有一个使用 websocket 连接到我的 goserver 的应用程序。我使用这段代码创建了一个 websocket 连接 - https://github.com/gorilla/websocket/tree/master/examples/chat

http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
        serveWs(hub, w, r)
    })

当我收到 POST 调用时,我需要向所有设备广播消息 - “Order Received:” productNum。 post请求中收到productNum。

http.HandleFunc("/post",BroadcastMessage())

我想知道我需要在 BroadcastMessage() 中写什么才能做到这一点。请帮忙。

最佳答案

在 BroadcastMessage 中,发送一个 []byte 到 hub 的广播 channel :

 hub.broadcast <- message

关于rest - 戈朗 : Broadcast messages to websocket when a particular POST JSON is received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40526628/

相关文章:

java - Spring框架启动不存在?

java - 为什么我的服务器 (Go) 无法解密客户端 (Java) 消息?

java - 在 Java 中从 URL 中读取 XML 字符串

JavaScript 库,如 JsonViewer

django - 为什么 request.POST 对象是不可变的?

c# - 检索包含 XML 的 HTTP POST 请求

javascript - 重定向而不是更改文本和发布

go - 利用goroutines和channel进行从上到下的树构建功能

go - 使用 Goroutine 订阅 MQTT 不打印消息

json - 无法从具有复合键的域类中获取其余API