Class: ActionCable::Connection::Base
- Inherits:
-
Object
- Object
- ActionCable::Connection::Base
- Defined in:
- lib/anycable/rails/actioncable/connection.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Class Method Summary collapse
Instance Method Summary collapse
-
#close ⇒ Object
rubocop:enable Metrics/MethodLength.
-
#fetch_identifier(name) ⇒ Object
Fetch identifier and deserialize if neccessary.
-
#handle_channel_command(identifier, command, data) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #handle_close ⇒ Object
- #handle_open ⇒ Object
-
#identifiers_hash ⇒ Object
Generate identifiers info.
- #identifiers_json ⇒ Object
-
#initialize(socket, identifiers: '{}', subscriptions: []) ⇒ Base
constructor
A new instance of Base.
- #logger ⇒ Object
- #transmit(cable_message) ⇒ Object
Constructor Details
#initialize(socket, identifiers: '{}', subscriptions: []) ⇒ Base
Returns a new instance of Base.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 29 def initialize(socket, identifiers: '{}', subscriptions: []) @ids = ActiveSupport::JSON.decode(identifiers) @cached_ids = {} @env = socket.env @coder = ActiveSupport::JSON @socket = socket @subscriptions = ActionCable::Connection::Subscriptions.new(self) # Initialize channels if any subscriptions.each { |id| @subscriptions.fetch(id) } end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
12 13 14 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 12 def socket @socket end |
Class Method Details
.call(socket, **options) ⇒ Object
15 16 17 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 15 def call(socket, **) new(socket, **) end |
.identified_by(*identifiers) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 19 def identified_by(*identifiers) super Array(identifiers).each do |identifier| define_method(identifier) do instance_variable_get(:"@#{identifier}") || fetch_identifier(identifier) end end end |
Instance Method Details
#close ⇒ Object
rubocop:enable Metrics/MethodLength
79 80 81 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 79 def close socket.close end |
#fetch_identifier(name) ⇒ Object
Fetch identifier and deserialize if neccessary
102 103 104 105 106 107 108 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 102 def fetch_identifier(name) @cached_ids[name] ||= @cached_ids.fetch(name) do val = @ids[name.to_s] next val unless val.is_a?(String) GlobalID::Locator.locate(val) || val end end |
#handle_channel_command(identifier, command, data) ⇒ Object
rubocop:disable Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 61 def handle_channel_command(identifier, command, data) channel = subscriptions.fetch(identifier) case command when "subscribe" channel.handle_subscribe !channel.subscription_rejected? when "unsubscribe" subscriptions.remove_subscription(channel) true when "message" channel.perform_action ActiveSupport::JSON.decode(data) true else false end end |
#handle_close ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 52 def handle_close logger.info if access_logs? subscriptions.unsubscribe_from_all disconnect if respond_to?(:disconnect) true end |
#handle_open ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 42 def handle_open logger.info if access_logs? connect if respond_to?(:connect) rescue ActionCable::Connection::Authorization:: logger.info ('Rejected') if access_logs? close end |
#identifiers_hash ⇒ Object
Generate identifiers info. Converts GlobalID compatible vars to corresponding global IDs params.
89 90 91 92 93 94 95 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 89 def identifiers_hash identifiers.each_with_object({}) do |id, acc| obj = instance_variable_get("@#{id}") next unless obj acc[id] = obj.try(:to_gid_param) || obj end end |
#identifiers_json ⇒ Object
97 98 99 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 97 def identifiers_json identifiers_hash.to_json end |
#logger ⇒ Object
110 111 112 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 110 def logger Anycable.logger end |
#transmit(cable_message) ⇒ Object
83 84 85 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 83 def transmit() socket.transmit encode() end |