在 ActionCable 中找不到“MyChannel"订阅类 [英] Subscription class not found 'MyChannel' in ActionCable
问题描述
我在使用 Action Cable 时遇到问题,每当我运行我的程序时,我都会收到一个错误消息,指出 Subscription
Class not found ConversationChannel
I am facing a problem while working with Action Cable, whenever I run my program I receive an error that says Subscription
Class not found ConversationChannel
当我尝试发送消息时,我收到此日志
and when I try to send a message I get this log
成功升级到 WebSocket (REQUEST_METHOD: GET,HTTP_CONNECTION:升级,HTTP_UPGRADE:websocket)订阅类未找到:ConversationChannel"无法执行命令{命令"=>消息","标识符"=>"{\"channel\":\"ConversationChannel\"}","data"=>"{\"message\":[{\"name\":\"conversation_id\",\"value\":\"2\"},{\"name\":\"amitian_id\",\"value\":\"1\"},{\"name\":\"body\",\"value\":\"nmm\"}],\"action\":\说话\"}"})[运行时错误 - 无法找到具有标识符的订阅:{"channel":"ConversationChannel"}]:C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:74:in<代码>查找' |C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:53:inperform_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`阻塞调用'
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) Subscription class not found: "ConversationChannel" Could not execute command from {"command"=>"message", "identifier"=>"{\"channel\":\"ConversationChannel\"}", "data"=>"{\"message\":[{\"name\":\"conversation_id\",\"value\":\"2\"},{\"name\":\"amitian_id\",\"value\":\"1\"},{\"name\":\"body\",\"value\":\"nmm\"}],\"action\":\"speak\"}"}) [RuntimeError - Unable to find subscription with identifier: {"channel":"ConversationChannel"}]: 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 `block in invoke'
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
文件?
using the log file given above can anyone tell me why It can't find my conversation_channel.rb
file ?
推荐答案
我已经通过将频道文件上移一个目录来解决这个问题.
I have resolved it by moving the channel file up one directory.
来自 app/channels/application_cable/conversation_channel.rb
app/channels/conversation_channel.rb
这篇关于在 ActionCable 中找不到“MyChannel"订阅类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!