2017-06-17 38 views
1

我想获得第一个ActionCable示例运行,但是,我觉得从来没有建立从客户端到通道的连接。我几乎没有在这里提到的其他一切都遵循edge guidesActionCable从来没有建立连接通道

# 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); 
    } 
}); 

如果它有助于任何进一步的,我彪马总是以“单人模式”,在这里我不知道这可能是问题,也不知道如何解决它。

回答

1

所以这里是原因。我不知何故只是试图把所有东西都设置为默认值,看看它是否会起作用,而且确实如此。我再慢慢加入CONFIGS和代码后面想通了,到底是什么,这个问题,这是下面一行config/environments/development.rb

config.reload_classes_only_on_change = false 

将其设置为true解决了这个问题。

相关问题