diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-04-06 13:17:02 -0500 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-04-06 13:17:02 -0500 |
commit | 1c9d82dbf0e743534c5fa9be936eaa46c5b07523 (patch) | |
tree | 4fc5c31666fa033859150852cc5fd583420e32d8 /lib/action_cable/remote_connection.rb | |
parent | eec92d0229a7bceb62d49d58c70b5629fe140d7f (diff) | |
download | rails-1c9d82dbf0e743534c5fa9be936eaa46c5b07523.tar.gz rails-1c9d82dbf0e743534c5fa9be936eaa46c5b07523.tar.bz2 rails-1c9d82dbf0e743534c5fa9be936eaa46c5b07523.zip |
Add remote connection to talk over internal redis channel
Diffstat (limited to 'lib/action_cable/remote_connection.rb')
-rw-r--r-- | lib/action_cable/remote_connection.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/action_cable/remote_connection.rb b/lib/action_cable/remote_connection.rb new file mode 100644 index 0000000000..e2cb2d932c --- /dev/null +++ b/lib/action_cable/remote_connection.rb @@ -0,0 +1,38 @@ +module ActionCable + class RemoteConnection + class InvalidIdentifiersError < StandardError; end + + include Connection::Identifier + + delegate :redis, to: :server + + def initialize(server, ids) + @server = server + set_identifier_instance_vars(ids) + end + + def disconnect + message = { type: 'disconnect' }.to_json + redis.publish(internal_redis_channel, message) + end + + def identifiers + @server.connection_identifiers + end + + def redis + @redis ||= Redis.new(@server.redis_config) + end + + private + def set_identifier_instance_vars(ids) + raise InvalidIdentifiersError unless valid_identifiers?(ids) + ids.each { |k,v| instance_variable_set("@#{k}", v) } + end + + def valid_identifiers?(ids) + keys = ids.keys + identifiers.all? { |id| keys.include?(id) } + end + end +end |