Class: ActionCable::Connection::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/anycable/rails/actioncable/connection.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, identifiers: '{}', subscriptions: []) ⇒ Base

Returns a new instance of Base.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/anycable/rails/actioncable/connection.rb', line 30

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

#socketObject (readonly)

Returns the value of attribute socket.



13
14
15
# File 'lib/anycable/rails/actioncable/connection.rb', line 13

def socket
  @socket
end

Class Method Details

.call(socket, **options) ⇒ Object



16
17
18
# File 'lib/anycable/rails/actioncable/connection.rb', line 16

def call(socket, **options)
  new(socket, options)
end

.identified_by(*identifiers) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/anycable/rails/actioncable/connection.rb', line 20

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

#closeObject

rubocop:enable Metrics/MethodLength



80
81
82
# File 'lib/anycable/rails/actioncable/connection.rb', line 80

def close
  socket.close
end

#fetch_identifier(name) ⇒ Object

Fetch identifier and deserialize if neccessary



104
105
106
107
108
109
110
111
# File 'lib/anycable/rails/actioncable/connection.rb', line 104

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/anycable/rails/actioncable/connection.rb', line 62

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_closeObject



53
54
55
56
57
58
59
# File 'lib/anycable/rails/actioncable/connection.rb', line 53

def handle_close
  logger.info finished_request_message if access_logs?

  subscriptions.unsubscribe_from_all
  disconnect if respond_to?(:disconnect)
  true
end

#handle_openObject



43
44
45
46
47
48
49
50
51
# File 'lib/anycable/rails/actioncable/connection.rb', line 43

def handle_open
  logger.info started_request_message if access_logs?

  connect if respond_to?(:connect)
  send_welcome_message
rescue ActionCable::Connection::Authorization::UnauthorizedError
  logger.info finished_request_message('Rejected') if access_logs?
  close
end

#identifiers_hashObject

Generate identifiers info. Converts GlobalID compatible vars to corresponding global IDs params.



90
91
92
93
94
95
96
97
# File 'lib/anycable/rails/actioncable/connection.rb', line 90

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_jsonObject



99
100
101
# File 'lib/anycable/rails/actioncable/connection.rb', line 99

def identifiers_json
  identifiers_hash.to_json
end

#loggerObject



113
114
115
# File 'lib/anycable/rails/actioncable/connection.rb', line 113

def logger
  Anycable.logger
end

#transmit(cable_message) ⇒ Object



84
85
86
# File 'lib/anycable/rails/actioncable/connection.rb', line 84

def transmit(cable_message)
  socket.transmit encode(cable_message)
end