当接收到特定的POST JSON时,将消息广播到WebSocket。

huangapple go评论77阅读模式
英文:

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 &lt;- message

huangapple
  • 本文由 发表于 2016年11月10日 19:33:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/40526628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定