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 .
资产/javascripts/channels/chatrooms.js
//= require action_cable
//= require_self
//= require_tree .
this.App = {};
App.cable = ActionCable.createConsumer("/cable");
资产/javascripts/channels/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 | 对于 rails、ruby、puma、redis 的 grep,它们都已成功关闭(即没有僵尸进程)。什么可能导致服务器冻结?