2017-06-17 42 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解決了這個問題。

相關問題