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

def process_connection(connection)
  cable_connection = @connection_class.new(connection.env)
  cable_connection.connect

  if cable_connection.rejected?
    Rage.logger.debug { "An unauthorized connection attempt was rejected" }
  else
    connection.env["rage.identified_by"] = cable_connection.__identified_by_map
    connection.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:



107
108
109
110
111
112
113
114
115
116
# File 'lib/rage/cable/router.rb', line 107

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

  if @connection_can_disconnect
    cable_connection = @connection_class.new(connection.env, connection.env["rage.identified_by"])
    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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rage/cable/router.rb', line 88

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



40
41
42
43
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
# File 'lib/rage/cable/router.rb', line 40

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