【问题标题】:Rails Action Cable not working since upgraded from 5.1 to 5.2从 5.1 升级到 5.2 后,Rails Action Cable 无法正常工作
【发布时间】:2019-03-16 04:08:13
【问题描述】:

我将 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')

日志 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});
  }
});

【问题讨论】:

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


    【解决方案1】:

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

    config/environments/development.rb

    config.reload_classes_only_on_change = false
    

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2020-08-20
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多