1
我想获得第一个ActionCable示例运行,但是,我觉得从来没有建立从客户端到通道的连接。我几乎没有在这里提到的其他一切都遵循edge guides。ActionCable从来没有建立连接通道
# 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
显示为有效的Subscription对象(尽管大多数属性都是函数,但它并没有太大帮助)。
// 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);
}
});
如果它有助于任何进一步的,我彪马总是以“单人模式”,在这里我不知道这可能是问题,也不知道如何解决它。