我有一个使用 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/