从5.1升级到5.2后,Rails Action Cable无法正常工作

时间:2018-10-11 06:47:40

标签: ruby-on-rails ruby ruby-on-rails-5 actioncable

我将我的Rails应用程序从5.1.4升级到了5.2.1,并且Action Cable正常运行,但自升级以来无法正常工作。 Websocket似乎工作正常,但是Channel无法开始传输和流式传输。

自从与Rails 5.1.4一起使用以来,我的开发服务器就没有问题,并且在Rails 5.2.1上正常运行的空白应用程序包括开发电缆在我的开发服务器上。

我不知道为什么频道无法启动。

Log在Rails 5.1.4上运行良好。

Started GET "/cable" for 10.0.2.2 at 2018-10-10 15:33:41 +0900
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/cable/" [WebSocket] for 10.0.2.2 at 2018-10-10 15:33:41 +0900
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Registered connection (Z2lkOi8vc2lzL1VzZXIvMQ)
CommentChannel is transmitting the subscription confirmation
CommentChannel is streaming from comment_channel_3
CommentChannel#speak({"comment"=>"43", "commentable_id"=>3, "user_id"=>1})
  Group Load (0.5ms)  SELECT  `groups`.* FROM `groups` WHERE `groups`.`inappropriate` = 0 AND `groups`.`id` = 3 LIMIT 1
   (0.1ms)  BEGIN
  User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  SQL (0.5ms)  INSERT INTO `comments` (`body`, `commentable_id`, `user_id`, `created_at`, `updated_at`, `commentable_type`) VALUES ('43', 3, 1, '2018-10-10 15:33:45', '2018-10-10 15:33:45', 'Group')

Log Rails 5.2.1。关于CommentChannel的日志没有显示。

Started GET "/cable/" [WebSocket] for 10.0.2.2 at 2018-10-11 15:13:56 +0900
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/channels/application_cable/connection.rb:14
Registered connection (Z2lkOi8vc2lzL1VzZXIvMQ)
Started GET "/cable" for 10.0.2.2 at 2018-10-11 15:14:09 +0900
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/cable/" [WebSocket] for 10.0.2.2 at 2018-10-11 15:14:09 +0900

app / channels / application_cable / connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private

    def find_verified_user
      if current_user = User.find_by(id: cookies.signed[:user_id])
        current_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

app / channels / application_cable / channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

app / channels / comment_channel.rb

class CommentChannel < ApplicationCable::Channel
  def subscribed
    stream_from "comment_channel_#{params[:id]}"
  end

  def unsubscribed; end

  def speak(data)
    # update comment
  end

  def read(data)
  end
end

app / assets / javascripts / cable.js

//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();
}).call(this);

comment.js

App.cable.subscriptions.create( {channel: 'CommentChannel', id: $('#comments').data('commentableId') }, {
  connected() {},
    // Called when the subscription is ready for use on the server

  disconnected() {},
    // Called when the subscription has been terminated by the server

  received(data) {
    this.perform('read', { group_id: $('#comments').data('commentableId') });

  },
  speak(comment, commentable_id, user_id){
    return this.perform('speak', {comment, commentable_id, user_id});
  }
});

1 个答案:

答案 0 :(得分:1)

这是问题所在。删除此行后,它可以正常工作。

config / environments / development.rb

config.reload_classes_only_on_change = false