跳至內容 跳至搜尋

Action Cable 遠端連線

如果你需要中斷特定連線,你可以透過 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) 所建立的連線,因為它使用了所有這些伺服器所訂閱的內部頻道。

預設情況下,伺服器會傳送設定為 true 的「中斷」訊息加上「重新連線」旗標。你可以透過指定 reconnect 選項來覆寫它

ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect(reconnect: false)
命名空間
方法
N
W

屬性

[R] server

類別公開方法

new(server)

# File actioncable/lib/action_cable/remote_connections.rb, line 34
def initialize(server)
  @server = server
end

執行個體公開方法

where(identifier)

# File actioncable/lib/action_cable/remote_connections.rb, line 38
def where(identifier)
  RemoteConnection.new(server, identifier)
end