我試圖找到一種方法來檢測用戶的最後一個窗口或選項卡正在關閉,所以我可以做一些清理;但我似乎無法找到一種方法來檢測它實際上是用戶的最後一個連接。我有一個跟蹤與我的頻道連接的模型,並且我不想在連接斷開時刪除用戶的連接記錄,前提是他們有其他活動選項卡打開連接。檢測最後一個窗口/ Tab關閉ActionCable
所以我需要檢查每個斷開連接,看看它們是否是具有相同標識符的其他活動連接。
我試着在disconnect方法中設置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] [[email protected]] UserChannel stopped streaming from user:Z2lkOi8vYWxseWNoYXQvVXNlci80Nw
[ActionCable] [[email protected]] #<ActionCable::RemoteConnections::RemoteConnection:0x00000007062690>
[ActionCable] [swa[email protected]] #<Set:0x00000007791b78>
[ActionCable] [[email protected]] #<Set: {:current_user}>
我已經解決這一迄今爲止建立的模型只是爲了跟蹤他們的連接爲每個頻道打開和關閉。但是它增加了很多開銷,而且管理起來很麻煩。
任何人都可以提出一種方法來管理?我一直在搜索ActionCable的API文檔,並且空着。