Rails 5 ActionCable问题,为Web套接字运行多个通道

时间:2016-05-24 17:33:25

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

snlamm我在Rails应用程序中实现多个Web套接字通道时遇到问题。错误是服务器在以下响应后冻结:

Started GET "/cable" for ::1 at 2016-05-24 11:42:16 -0400
Started GET "/cable/" [WebSocket] for ::1 at 2016-05-24 11:42:16 -0400
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)

注意:我们的浏览器控制台不会抛出任何错误。我们使用的是Chrome v 50.0(最新版本)。

但是,如果我们重新启动浏览器和服务器,则会出现不一致的情况:

Started GET "/cable" for ::1 at 2016-05-24 11:45:54 -0400
Started GET "/cable/" [WebSocket] for ::1 at 2016-05-24 11:45:54 -0400
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
ItemsChannel is transmitting the subscription confirmation
ItemsChannel is streaming from items
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from messages

一切正常。

如上所述,此修复程序不一致,而且通常我们的服务器冻结。我们注意到,当我们只运行一个频道时没有问题。

以下是项目频道的代码,它是我们添加的两个频道之一。两个频道的代码几乎相同:

路线

mount ActionCable.server => '/cable'

渠道/ application_cable / channel.rb

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

渠道/ application_cable / connection.rb

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

渠道/ items_channel.rb

class ItemsChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'items'
  end
end

视图/命令/ show.html.erb

这是我们对显示项目的附加频道的看法。

<div id="item-list">
      <h2>Items: <% @order.items.each do |item| %></h2>
      <li><%= item.name %> - $<%= item.cost %></li>
      <% end %>
    </div>
  </ol>
  <div id="item-errors">
  </div>
  <br>
  <h3>Time until ordered: <%= ((@order.expiration - @order.date_ordered) / 60).round %> minutes</h3></div>

  <%= form_for @item, remote: true do |f| %>
    <%= f.hidden_field :order_id, value: @order.id, id: "order-id" %>
  <span id='item-content-reset'>
    Item name: <%= f.text_field :name, id: 'item-name' %>
    Cost: <%= f.text_field :cost, id: 'item-cost' %>
  </span>
    <%= f.submit "Add Item", :id => 'item-create-btn' %>
  <% end %>
</div>

控制器/ items_controller.rb

class ItemsController < ApplicationController
  def create
    @item = Item.new(item_params)
    @item.user = @current_user
    if @item.save
      ActionCable.server.broadcast 'items',
        name: @item.name,
        cost: @item.cost
      head :ok
    else
      error_message = @item.errors.messages
      render partial: 'shared/errors', locals: { errors: flash.now[:alert] = "Item " + error_message[:name][0] }
    end
  end

  private

  def item_params
    params.require(:item).permit(:order_id, :cost, :name)
  end
end

资产/ application.js中

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require bootstrap-switch
// require jquery.session
// require turbolinks
//= require_tree .

资产/ Javascript角/信道/ chatrooms.js

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

this.App = {};

App.cable = ActionCable.createConsumer("/cable");

资产/ Javascript角/信道/ items.js

App.items = App.cable.subscriptions.create('ItemsChannel', {
  received: function(data) {
    return $("#item-list").append(this.renderItem(data));
  },
  renderItem: function(data) {
    return "<li> " + data.name + " - " + "$" + data.cost + "</li>";
  }
});

结束服务器之后我们ps aux | grep for rails,ruby,puma,redis和他们都已成功关闭(即没有僵尸进程)。什么可能导致服务器冻结?

1 个答案:

答案 0 :(得分:1)

这听起来像以下错误(Actioncable deadlock with multiple channels #24741)。您可以在前面的链接中获取补丁。如果您正在开发中工作,您还可以尝试在development.rb中设置config.eager_load = true,这对我有用。