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, env) ⇒ 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



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

def process_connection(connection, env)
  cable_connection = @connection_class.new(env)
  cable_connection.__connect(connection, @protocol)

  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, env) ⇒ Object

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

Parameters:



128
129
130
131
132
133
134
135
# File 'lib/rage/cable/router.rb', line 128

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

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

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

Calls the handler method on the specified channel.

Parameters:

  • env (Hash) —

    the Rack environment

  • 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



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

def process_message(_, env, identifier, action_name, data)
  channel = 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, env, identifier, channel_name, params) ⇒ :invalid, ...

Calls the subscribed method on the specified channel.

Parameters:

  • connection (Rage::Cable::WebSocketConnection) —

    the connection object

  • env (Hash) —

    the Rack environment

  • 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



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
81
82
# File 'lib/rage/cable/router.rb', line 46

def process_subscription(connection, env, identifier, channel_name, params)
  channel_class = @channels_map[channel_name] || begin
    begin
      klass = @namespace.const_get(channel_name, false) unless channel_name.start_with?("::")
    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(@protocol).tap do |available_actions|
      Rage.logger.debug { "Compiled #{klass.name}. Available remote actions: #{available_actions}." }
    end

    @channels_map[channel_name] = klass
  end

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

  if channel.subscription_rejected?
    Rage.logger.debug { "#{channel_class} 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_class} is transmitting the subscription confirmation" }
    env["rage.cable"][identifier] = channel
    :subscribed
  end
end

#process_unsubscription(_, env, identifier) ⇒ Object

Runs the unsubscribed method on a specific channel without closing the connection.

Parameters:

  • env (Hash) —

    the Rack environment

  • identifier (String) —

    the identifier of the subscription



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

def process_unsubscription(_, env, identifier)
  channel = env["rage.cable"].delete(identifier)
  unless channel
    Rage.logger.debug { "Unable to find the subscription" }
    return
  end

  channel.__run_action(:unsubscribed)
end