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
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 28 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.
11 12 13 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 11 def socket @socket end |
Class Method Details
.create(socket, **options) ⇒ Object
14 15 16 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 14 def create(socket, **) new(socket, **) end |
.identified_by(*identifiers) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 18 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
72 73 74 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 72 def close socket.close end |
#fetch_identifier(name) ⇒ Object
Fetch identifier and deserialize if neccessary
95 96 97 98 99 100 101 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 95 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 54 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
48 49 50 51 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 48 def handle_close subscriptions.unsubscribe_from_all disconnect if respond_to?(:disconnect) end |
#handle_open ⇒ Object
41 42 43 44 45 46 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 41 def handle_open connect if respond_to?(:connect) rescue ActionCable::Connection::Authorization:: close end |
#identifiers_hash ⇒ Object
Generate identifiers info. Converts GlobalID compatible vars to corresponding global IDs params.
82 83 84 85 86 87 88 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 82 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
90 91 92 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 90 def identifiers_json identifiers_hash.to_json end |
#logger ⇒ Object
103 104 105 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 103 def logger Anycable::Rails.logger end |
#transmit(cable_message) ⇒ Object
76 77 78 |
# File 'lib/anycable/rails/actioncable/connection.rb', line 76 def transmit() socket.transmit encode() end |