我使用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
路由。
你想通了吗?如果不是,如果在浏览器的JS控制台中输入'App.status_monitor'会发生什么? –