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

时间:2016-11-08 19:56:35

标签: ruby-on-rails actioncable

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

5 个答案:

答案 0 :(得分:7)

我在 /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
  

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

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

答案 1 :(得分:2)

您可以使用unsubscribe()功能:

App.example = App.cable.subscriptions.create(..);
App.example.unsubscribe();

答案 2 :(得分:0)

我的工作是创建一条新路线,仅用于断开连接。

def disconnection
    ActionCable.server.remote_connections.where(connected_user: user_params['email']).disconnect

   render json: {}, status: 200 
end

客户端必须调用端点......类似于

PUT  /api/cable/disconnection

答案 3 :(得分:0)

断开与客户端的连接

我偶然发现了这个问题。但我无法相信没有简单的方法可以断开websocket连接与客户端的连接(无需进行API调用)。幸运的是,这对我有用:

// Create consumer
window.cable = ActionCable.createConsumer(...)

// Subscribe to channels
window.cable.subscriptions.create('SomeChannel', ...);

// At some point we want to disconnect (e.g. when user logs out)
window.cable.subscriptions.consumer.disconnect();

答案 4 :(得分:0)

要与客户端断开连接(在js中),请致电

App.cable.disconnect();

要断开与服务器端的连接-请参阅@prograils的答案

相关问题