2

我已经建立了一个像这里这样的小型聊天应用程序: https ://github.com/chrismccord/phoenix_chat_example/blob/master/web/channels/room_channel.ex

并且无法弄清楚如何在一个主题中向所有用户广播一条消息。在上面的应用程序中(没有像我使用的那样更新到 v0.13),我该怎么做?以下是我没有运气的尝试:

Phoenix.PubSub.broadcast Chat.PubSub, "new:msg", "hello from the console"
Phoenix.PubSub.broadcast Chat.Endpoint, "new:msg", "hello from the console"
Phoenix.PubSub.broadcast Chat.RoomChannel, "new:msg", "hello from the console"

它们都不起作用......其中一些抛出异常:(

4

1 回答 1

8

由于您使用的是 0.13,因此您需要从端点广播,并且您需要提供主题、事件和有效负载(作为地图)。尝试这个:

Chat.Endpoint.broadcast("rooms:lobby", "new:msg", %{message: "hello from the console"})

此代码段对您的频道和客户端代码进行了一些假设,因此如果它不起作用,请提供您的路由器、频道和 js 代码,以便我可以进一步提供帮助。

于 2015-05-27T20:35:39.423 回答