【发布时间】:2017-11-20 00:37:14
【问题描述】:
我正在尝试运行第一个 ActionCable 示例,但是,我觉得从来没有建立从客户端到通道的连接。在此处未提及的所有其他内容上,我几乎都遵循了edge guides。
# app/channels/notifications_channel.rb
class NotificationsChannel < ApplicationCable::Channel
def subscribed
byebug
stream_from "notifications"
end
def unsubscribed
stop_all_streams
end
def receive(data)
ActionCable.server.broadcast("notifications", data)
end
end
byebug 从未被调用过。实际上,我可以将一堆语法乱码放入该文件中,并且不会在任何地方看到错误。就像文件永远不会加载一样。
这里是 javascript,如果它会建立连接,我希望看到“已连接”警报,但这种情况永远不会发生。该文件已加载并正确执行,因为我可以在浏览器控制台中检查 App 变量,它会将 App.notifications 显示为有效的订阅对象(尽管大多数属性都是函数,但它对调试没有帮助)。
// app/assets/javascripts/channels/notifications.js
App.notifications = App.cable.subscriptions.create("NotificationsChannel", {
connected: function() {
// Called when the subscription is ready for use on the server
alert('connected');
},
disconnected: function() {
// Called when the subscription has been terminated by the server
},
received: function(data) {
alert(data)
// Called when there's incoming data on the websocket for this channel
$("#notifications").prepend(data.html);
}
});
如果它有任何帮助,我的 Puma 总是以“单一模式”启动,我不确定这是否可能是问题,也不知道如何解决它。
【问题讨论】:
标签: ruby-on-rails websocket real-time puma actioncable