2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-25 23:42:21 -04:00
|
|
|
require "active_support/core_ext/module/redefine_method"
|
|
|
|
|
2015-04-06 14:31:17 -04:00
|
|
|
module ActionCable
|
2016-02-02 11:16:41 -05:00
|
|
|
# If you need to disconnect a given connection, you can go through the
|
|
|
|
# RemoteConnections. You can find the connections you're looking for by
|
|
|
|
# searching for the identifier declared on the connection. For example:
|
2015-07-07 16:53:58 -04:00
|
|
|
#
|
|
|
|
# module ApplicationCable
|
|
|
|
# class Connection < ActionCable::Connection::Base
|
|
|
|
# identified_by :current_user
|
|
|
|
# ....
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect
|
|
|
|
#
|
2016-02-02 11:16:41 -05:00
|
|
|
# This will disconnect all the connections established for
|
2016-02-13 21:05:04 -05:00
|
|
|
# <tt>User.find(1)</tt>, across all servers running on all machines, because
|
|
|
|
# it uses the internal channel that all of these servers are subscribed to.
|
2015-04-06 14:31:17 -04:00
|
|
|
class RemoteConnections
|
|
|
|
attr_reader :server
|
|
|
|
|
|
|
|
def initialize(server)
|
|
|
|
@server = server
|
|
|
|
end
|
|
|
|
|
|
|
|
def where(identifier)
|
|
|
|
RemoteConnection.new(server, identifier)
|
|
|
|
end
|
2015-07-07 16:53:58 -04:00
|
|
|
|
|
|
|
private
|
2016-02-02 11:16:41 -05:00
|
|
|
# Represents a single remote connection found via <tt>ActionCable.server.remote_connections.where(*)</tt>.
|
2016-04-21 02:01:08 -04:00
|
|
|
# Exists solely for the purpose of calling #disconnect on that connection.
|
2015-07-07 16:53:58 -04:00
|
|
|
class RemoteConnection
|
|
|
|
class InvalidIdentifiersError < StandardError; end
|
|
|
|
|
|
|
|
include Connection::Identification, Connection::InternalChannel
|
|
|
|
|
|
|
|
def initialize(server, ids)
|
|
|
|
@server = server
|
|
|
|
set_identifier_instance_vars(ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Uses the internal channel to disconnect the connection.
|
|
|
|
def disconnect
|
2021-03-14 14:17:02 -04:00
|
|
|
server.broadcast internal_channel, { type: "disconnect" }
|
2015-07-07 16:53:58 -04:00
|
|
|
end
|
|
|
|
|
2015-07-07 17:13:00 -04:00
|
|
|
# Returns all the identifiers that were applied to this connection.
|
2017-05-20 23:34:38 -04:00
|
|
|
redefine_method :identifiers do
|
2015-07-07 16:53:58 -04:00
|
|
|
server.connection_identifiers
|
|
|
|
end
|
|
|
|
|
2017-08-19 11:37:05 -04:00
|
|
|
protected
|
2015-07-07 16:53:58 -04:00
|
|
|
attr_reader :server
|
|
|
|
|
2017-08-19 11:37:05 -04:00
|
|
|
private
|
2015-07-07 16:53:58 -04:00
|
|
|
def set_identifier_instance_vars(ids)
|
|
|
|
raise InvalidIdentifiersError unless valid_identifiers?(ids)
|
2016-10-28 23:05:58 -04:00
|
|
|
ids.each { |k, v| instance_variable_set("@#{k}", v) }
|
2015-07-07 16:53:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_identifiers?(ids)
|
|
|
|
keys = ids.keys
|
|
|
|
identifiers.all? { |id| keys.include?(id) }
|
|
|
|
end
|
|
|
|
end
|
2015-04-06 14:31:17 -04:00
|
|
|
end
|
|
|
|
end
|