【问题标题】:Golang: Broadcast messages to websocket when a particular POST JSON is receivedGolang:收到特定 POST JSON 时向 websocket 广播消息
【发布时间】:2016-11-10 11:33:54
【问题描述】:

我有一个使用 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 呼叫时,我需要向所有设备广播一条消息 -“订单已收到:”productNum。在 post 请求中收到 productNum。

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

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

【问题讨论】:

  • 我已经编写了解析 json 的代码,并且我有 productNum 变量。我只需要广播消息。

标签: rest post go websocket broadcast


【解决方案1】:

在 BroadcastMessage 中,将[]byte 发送到集线器的广播频道:

 hub.broadcast <- message

【讨论】:

  • 非常感谢。像魅力一样工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多