未找到订阅类' MyChannel'在ActionCable中

时间:2017-06-11 17:41:58

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 actioncable

我在使用Action Cable时遇到问题,每当我运行程序时,都会收到一条错误消息,指出Subscription找不到类ConversationChannel

当我尝试发送消息时,我得到了这个日志

  

成功升级到WebSocket(REQUEST_METHOD:GET,   HTTP_CONNECTION:升级,HTTP_UPGRADE:websocket)订阅类   找不到:" ConversationChannel"无法执行命令   {"命令" = GT;"消息&#34 ;,   "标识符" = GT;" {\"信道\":\" ConversationChannel \"}&#34 ;,   "数据" =>" {\"消息\":[{\"名称\":\" conversation_id \& #34; \"值\":\" 2 \"},{\"名称\":\" amitian_id \& #34; \"值\":\" 1 \"},{\"名称\":\"主体\& #34; \"值\":\" NMM \"}],\"操作\":\"说话\& #34;}"})   [RuntimeError - 无法找到带标识符的订阅:   {"信道":" ConversationChannel"}]:   C:/RailsInstaller/Ruby2.3.​​0/lib/ruby/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/connection/subscriptions.rb:74:在   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:在   execute_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:在   `阻止调用'

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');
});

connection.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文件?

3 个答案:

答案 0 :(得分:16)

我已经通过将频道文件移到一个目录来解决它。

来自app/channels/application_cable/conversation_channel.rbapp/channels/conversation_channel.rb

答案 1 :(得分:3)

您必须在客户端(JS)代码中提供完整的Module :: Class名称。对于例如如果您的ConversationChannel课程在ApplicationCable模块中,那么在您的客户端而不仅仅是ConversationChannel,您需要执行ApplicationCable::ConversationChannel

答案 2 :(得分:1)

您的文件需要命名为conversation_channel.rb,因为snake_case是频道文件名的rails约定。