在 ActionCable 中找不到订阅类“MyChannel”
Posted
技术标签:
【中文标题】在 ActionCable 中找不到订阅类“MyChannel”【英文标题】:Subscription class not found 'MyChannel' in ActionCable 【发布时间】:2017-11-13 04:26:45 【问题描述】:我在使用 Action Cable 时遇到了一个问题,每当我运行我的程序时,我都会收到一条错误消息:Subscription
Class not found ConversationChannel
当我尝试发送消息时,我会收到此日志
成功升级到 WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: 升级, HTTP_UPGRADE: websocket) 订阅类 未找到:“ConversationChannel”无法执行来自的命令 “命令”=>“消息”, "标识符"=>"\"频道\":\"ConversationChannel\"", "data"=>"\"message\":[\"name\":\"conversation_id\",\"value\":\"2\",\"name\":\"amitian_id \",\"value\":\"1\",\"name\":\"body\",\"value\":\"nmm\"],\"action\":\ “说话\””) [RuntimeError - 无法找到带有标识符的订阅: “频道”:“对话频道”]: C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:74:in
find' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:53:in
perform_action' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:17:inexecute_command' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/base.rb:88:in
dispatch_websocket_message' | C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/server/worker.rb:58:in `调用中的阻塞'
ConversationChannel.rb
class ConversationChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
stream_from "conversations-#current_amitian.id"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
stop_all_streams
end
def speak(data)
message_params = data['message'].each_with_object() do |el, hash|
hash[el.values.first] = el.values.last
end
ActionCable.server.broadcast(
"conversations-#current_amitian.id",
message: message_params
)
end
end
conversation.js
App.conversation = App.cable.subscriptions.create("ConversationChannel",
connected: function()
,
disconnected: function()
,
received: function(data)
console.log(data['message']);
,
speak: function(message)
return this.perform('speak' ,
message: message
);
);
$(document).on('submit', '.new_message', function(e)
e.preventDefault();
var values = $(this).serializeArray();
App.conversation.speak(values);
$(this).trigger('reset');
);
连接.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_amitian
def connect
self.current_amitian = find_verified_amitian
end
protected
def find_verified_amitian
if(current_amitian = env['warden'].amitian)
current_amitian
else
reject_unauthorized_connection
end
end
end
end
使用上面给出的日志文件谁能告诉我为什么找不到我的conversation_channel.rb
文件?
【问题讨论】:
【参考方案1】:我已经通过将频道文件上移一个目录解决了这个问题。
来自app/channels/application_cable/conversation_channel.rb
给app/channels/conversation_channel.rb
【讨论】:
【参考方案2】:您必须在客户端 (JS) 代码中提供完整的 Module::Class 名称。例如如果您的 ConversationChannel
类在 ApplicationCable
模块中,那么在您的客户端中,而不仅仅是 ConversationChannel
您需要执行 ApplicationCable::ConversationChannel
【讨论】:
我发现我还需要包含 Channel 类:ApplicationCable::Channel::CountsChannel
【参考方案3】:
您的文件需要命名为conversation_channel.rb
,因为snake_case 是通道文件名的rails 约定。
【讨论】:
在您的帖子中您提到您的文件名为ConversationChannel.rb
。不是这样吗?检查文件名中是否有任何拼写错误?以上是关于在 ActionCable 中找不到订阅类“MyChannel”的主要内容,如果未能解决你的问题,请参考以下文章