2017-05-19 24 views
2

我使用Smart Admin theme运行简单的Rails应用程序。 我尝试了很多,但无法在我的应用程序上运行Action电缆。ActionCable没有仅在频道中调用#订阅注册连接

my_own_channel.rb

class MyOwnChannel < ApplicationCable::Channel 
    def subscribed 
    stream_for 'my_own_channel' 
    end 
    def unsubscribed; end 
end 

channel.rb

module ApplicationCable 
    class Channel < ActionCable::Channel::Base 
    end 
end 

connection.rb

module ApplicationCable 
    class Connection < ActionCable::Connection::Base 
    identified_by :current_user 

    def connect 
     self.current_user = find_verified_user 
     logger.add_tags 'ActionCable', current_user.email 
    end 

    protected 

    def find_verified_user 
     if verified_user = env['warden'].user 
     verified_user 
     else 
     reject_unauthorized_connection 
     end 
    end 
    end 
end 

../app/assets/javascripts/channels/my_own.js

App.status_monitor = App.cable.subscriptions.create("MyOwnChannel", { 
    received: function(data) { 
    alert('Received....'); 
    }, 
    connected: function() { 
    alert('connected'); 
    }, 
    disconnected: function() { 
    alert('disconnected now'); 
    } 
}); 

../app/assets/javascripts/cable.js

//= require action_cable 
//= require_self 
//= require_tree ./channels 

(function() { 
    this.App || (this.App = {}); 

    App.cable = ActionCable.createConsumer(); 

}).call(this); 

做这一切后,在日志中我没有看到流。 在我看到的日志中:

Started GET "/cable" for 127.0.0.1 at 2017-05-19 22:20:36 +0630 
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-05-19 22:20:36 +0630 
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) 
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] 
[ActionCable] [[email protected]] Registered connection (abcdefghijklmnopqrstuvwxyz) 

而不是流连接在那里。 另一个问题是,我看到connection.rb中的connect方法被调用,但是MyOwnChannel中的subscribe方法没有被调用。 我不知道什么是缺少的,我也在routes.rb文件中安装了/cable路由。

+0

你想通了吗?如果不是,如果在浏览器的JS控制台中输入'App.status_monitor'会发生什么? –

回答

1

想通了。 真正的问题是与Pace.js 我只是评论它和行动电缆开始工作。没有弄清楚2 js文件之间的真实冲突是什么。但现在工作。

+0

真的很感谢。在我花了一整天的时间修复它之后,它帮了很大忙。 –

0

这对我来说也是一个问题。删除pace.js将修复它,但你也可以禁用的WebSockets的跟踪也应该做的伎俩:

// set this before you load pace.js 
window.paceOptions = { 
    ajax: { 
    trackWebSockets: false 
    } 
} 

(见源here

也有一个在速度项目的开放问题: https://github.com/HubSpot/pace/issues/411