Class: Rage::Cable::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/cable/router.rb

Instance Method Summary collapse

Instance Method Details

#process_connection(connection) ⇒ true, false

Calls the connect method on the Connection class to handle authentication.

Parameters:

Returns:

  • (true)

    if the connection was accepted

  • (false)

    if the connection was rejected



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rage/cable/router.rb', line 16

def process_connection(connection)
  env = connection.env

  cable_connection = @connection_class.new(env)
  Rage::Telemetry.tracer.span_cable_connection_process(connection: cable_connection, action: :connect, env:) do
    cable_connection.connect
  end

  if cable_connection.rejected?
    Rage.logger.debug { "An unauthorized connection attempt was rejected" }
  else
    env["rage.identified_by"] = cable_connection.__identified_by_map
    env["rage.cable"] = {}
  end

  !cable_connection.rejected?
end

#process_disconnection(connection) ⇒ Object

Runs the unsubscribed methods on all the channels the client is subscribed to.

Parameters:



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rage/cable/router.rb', line 111

def process_disconnection(connection)
  env = connection.env

  env["rage.cable"]&.each do |_, channel|
    channel.__run_action(:unsubscribed)
  end

  cable_connection = @connection_class.new(env, env["rage.identified_by"])

  Rage::Telemetry.tracer.span_cable_connection_process(connection: cable_connection, action: :disconnect, env:) do
    cable_connection.disconnect
  end
end

#process_message(connection, identifier, action_name, data) ⇒ :no_subscription, ...

Calls the handler method on the specified channel.

Parameters:

  • connection (Rage::Cable::WebSocketConnection)

    the connection object

  • identifier (String)

    the identifier of the subscription

  • action_name (Symbol)

    the name of the handler method

  • data (Object)

    the data sent by the client

Returns:

  • (:no_subscription)

    if the client is not subscribed to the specified channel

  • (:unknown_action)

    if the action does not exist on the specified channel

  • (:processed)

    if the message has been successfully processed



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rage/cable/router.rb', line 92

def process_message(connection, identifier, action_name, data)
  channel = connection.env["rage.cable"][identifier]
  unless channel
    Rage.logger.debug { "Unable to find the subscription" }
    return :no_subscription
  end

  if channel.__has_action?(action_name)
    channel.__run_action(action_name, data)
    :processed
  else
    Rage.logger.debug { "Unable to process #{channel.class.name}##{action_name}" }
    :unknown_action
  end
end

#process_subscription(connection, identifier, channel_name, params) ⇒ :invalid, ...

Calls the subscribed method on the specified channel.

Parameters:

  • connection (Rage::Cable::WebSocketConnection)

    the connection object

  • identifier (String)

    the identifier of the subscription

  • channel_name (String)

    the name of the channel class

  • params (Hash)

    the params hash associated with the subscription

Returns:

  • (:invalid)

    if the subscription class does not exist

  • (:rejected)

    if the subscription was rejected

  • (:subscribed)

    if the subscription was accepted



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rage/cable/router.rb', line 44

def process_subscription(connection, identifier, channel_name, params)
  channel_class = @channels_map[channel_name] || begin
    begin
      klass = Object.const_get(channel_name)
    rescue NameError
      nil
    end

    if klass.nil? || !klass.ancestors.include?(Rage::Cable::Channel)
      Rage.logger.debug { "Subscription class not found: #{channel_name}" }
      return :invalid
    end

    klass.__register_actions.tap do |available_actions|
      Rage.logger.debug { "Compiled #{channel_name}. Available remote actions: #{available_actions}." }
    end

    @channels_map[channel_name] = klass
  end

  channel = channel_class.new(connection, params, connection.env["rage.identified_by"])
  channel.__run_action(:subscribed)

  if channel.subscription_rejected?
    Rage.logger.debug { "#{channel_name} is transmitting the subscription rejection" }
    # if the subscription is rejected in the `subscribed` method, ActionCable will additionally run
    # the `unsubscribed` method; this makes little sense to me as the client was never subscribed in
    # the first place; additionally, I don't think this behaviour is documented anywhere;
    # so, I'm going to leave this line commented out for now;
    # channel.__run_action(:unsubscribed)
    :rejected
  else
    Rage.logger.debug { "#{channel_name} is transmitting the subscription confirmation" }
    connection.env["rage.cable"][identifier] = channel
    :subscribed
  end
end