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

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

您可以使用unsubscribe()函数:

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

我在/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控制台中运行。

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

 def disconnection ActionCable.server.remote_connections.where(connected_user: user_params['email']).disconnect render json: {}, status: 200 end 

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

 PUT /api/cable/disconnection 

断开与客户端的连接

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

 // Create consumer window.cable = ActionCable.createConsumer(...) // Subscribe to channels window.cable.subscriptions.create('SomeChannel', ...); // At some point we want to disconnect (eg when user logs out) window.cable.subscriptions.consumer.disconnect();