英文:
Golang: Broadcast messages to websocket when a particular POST JSON is received
问题
我有一个连接到我的goserver的应用程序,使用websocket进行连接。我使用了这段代码来创建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。productNum是在POST请求中接收到的。
http.HandleFunc("/post", BroadcastMessage())
我想知道在BroadcastMessage()中需要编写什么来实现这个功能。请帮忙。
英文:
I have an app that is connected to my goserver using websocket . I used this code to create a websocket connection - https://github.com/gorilla/websocket/tree/master/examples/chat
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
When I receive a POST call I need to broadcast a message - "Order Received: " productNum to all the devices. productNum is received in the post request.
http.HandleFunc("/post",BroadcastMessage())
I want to know what I need to write in BroadcastMessage() to do this. Please help.
答案1
得分: 2
在BroadcastMessage中,将[]byte
发送到中心的广播通道:
hub.broadcast <- message
英文:
In BroadcastMessage, send a []byte
to the hub's broadcast channel:
hub.broadcast <- message
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论