ActionCable 仅在频道中不调用#subscribe 注册连接
ActionCable not calling #subscribe in channel only Registering connection
我运行宁简单 Rails 应用 Smart Admin theme。
我尝试了很多,但无法在我的应用程序上 运行 动作电缆。
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" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
[ActionCable] [abcdefghijklmn@gmail.com] Registered connection (abcdefghijklmnopqrstuvwxyz)
而且那里没有连接的流。
另一个问题是,我看到 connection.rb 中的连接方法被调用,但 MyOwnChannel 中的订阅方法没有被调用。
我不知道缺少什么,我还在 routes.rb 文件中安装了 /cable
路由。
想通了。
真正的问题是 Pace.js
我简单地评论了它,Action 电缆就开始工作了。没有弄清楚2个js文件之间真正的冲突是什么。但暂时有效。
这对我来说也是一个问题。删除 pace.js
将修复它,但您也可以禁用对 websockets 的跟踪,这也应该可以解决问题:
// set this before you load pace.js
window.paceOptions = {
ajax: {
trackWebSockets: false
}
}
(参见来源 here)
pace 项目还有一个未解决的问题:
https://github.com/HubSpot/pace/issues/411
我运行宁简单 Rails 应用 Smart Admin theme。 我尝试了很多,但无法在我的应用程序上 运行 动作电缆。
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" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
[ActionCable] [abcdefghijklmn@gmail.com] Registered connection (abcdefghijklmnopqrstuvwxyz)
而且那里没有连接的流。
另一个问题是,我看到 connection.rb 中的连接方法被调用,但 MyOwnChannel 中的订阅方法没有被调用。
我不知道缺少什么,我还在 routes.rb 文件中安装了 /cable
路由。
想通了。 真正的问题是 Pace.js 我简单地评论了它,Action 电缆就开始工作了。没有弄清楚2个js文件之间真正的冲突是什么。但暂时有效。
这对我来说也是一个问题。删除 pace.js
将修复它,但您也可以禁用对 websockets 的跟踪,这也应该可以解决问题:
// set this before you load pace.js
window.paceOptions = {
ajax: {
trackWebSockets: false
}
}
(参见来源 here)
pace 项目还有一个未解决的问题: https://github.com/HubSpot/pace/issues/411