如何关闭动作电缆中的连接? [英] How to close connection in Action cable?
本文介绍了如何关闭动作电缆中的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Action cable (rails 5) 中断开客户端的连接?我希望用户完全断开连接(类似于他关闭选项卡时).
How to disconnect a client in Action cable (rails 5)? I would like the user to be completely disconnected (similar to when he closes the tab).
推荐答案
断开客户端与 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
从客户端断开连接
如果你想断开用户与客户端的连接,你可以在你的 javascript 中使用 disconnect
和 unsubscribe
函数:
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();
这篇关于如何关闭动作电缆中的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文