如何在Action电缆中关闭连接?

Sle*_*der 4 ruby-on-rails actioncable

如何在Action电缆(导轨5)中断开客户端?我希望用户完全断开连接(类似于他关闭标签时).

sie*_*y22 10

断开客户端与 Rails 应用程序的连接

如果要将客户端与 rails 应用程序断开连接,请使用disconnect文档中描述的方法:https : //api.rubyonrails.org/classes/ActionCable/RemoteConnections.html

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

ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect
Run Code Online (Sandbox Code Playgroud)

从客户端断开连接

如果您想断开用户与客户端的连接,您可以在您的 javascript 中使用disconnectunsubscribe函数:

App.cable = ActionCable.createConsumer(...)

// Closes the websocket connection.
App.cable.disconnect();

// Unsubscribe from a actioncable subscription (without disconnecting the websocket connection)
App.example = App.cable.subscriptions.create(..);
App.example.unsubscribe();
Run Code Online (Sandbox Code Playgroud)

  • 这会删除一个或多个订阅 - 但它不会关闭 websocket。 (2认同)

pro*_*ils 9

我在/var/lib/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/remote_connections.rb中找到了这个

如果需要断开给定连接,可以通过RemoteConnections.您可以通过搜索连接上声明的标识符来查找要查找的连接.例如:

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

ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect
Run Code Online (Sandbox Code Playgroud)

这将断开
在所有计算机上运行的所有服务器上为User.find(1)建立的所有连接,因为它使用所有这些服务器都订阅的内部通道.

希望这会有用.看起来它甚至可以在Rails控制台中运行.