【发布时间】:2017-06-01 12:28:17
【问题描述】:
我正在尝试找到一种方法来检测用户的最后一个窗口或选项卡是否正在关闭,以便我可以进行一些清理;但我似乎无法找到一种方法来检测它实际上是用户的最后一个连接。我有一个模型可以跟踪与我的频道的连接,并且我不想在用户断开连接时删除用户的连接记录,如果他们有其他活动选项卡打开连接。
所以我需要以某种方式检查每个断开连接,看看它们是否是具有相同标识符的其他活动连接。
我尝试在断开连接方法中设置对 RemoteConnections 的检查。但是当它被调用时,似乎正在关闭的连接仍然在 RemoteConnections 下返回。
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.username
logger.debug self.current_user.username + " now connected."
end
def disconnect
self.close()
logger.debug ActionCable.server.remote_connections.where(current_user: current_user)
logger.debug ActionCable.server.remote_connections.where(current_user: current_user).identifiers
logger.debug ActionCable.server.remote_connections.where(current_user: current_user).identifiers.inspect()
end
end
即使使用该标识符的最后一个连接正在关闭,此设置也会返回以下内容:
[ActionCable] [swachtma@gmail.com] UserChannel stopped streaming from user:Z2lkOi8vYWxseWNoYXQvVXNlci80Nw
[ActionCable] [swachtma@gmail.com] #<ActionCable::RemoteConnections::RemoteConnection:0x00000007062690>
[ActionCable] [swachtma@gmail.com] #<Set:0x00000007791b78>
[ActionCable] [swachtma@gmail.com] #<Set: {:current_user}>
到目前为止,我一直在通过建立一个模型来解决这个问题,该模型只是为了跟踪每个频道打开和关闭的连接。但这会增加很多开销,而且管理起来很麻烦。
任何人都可以建议一种方法来管理这个吗?我一直在搜索 ActionCable 的 API 文档,结果一无所获。
【问题讨论】: