Class: Volt::Dispatcher

Inherits:
Object show all
Defined in:
lib/volt/tasks/dispatcher.rb

Overview

The task dispatcher is responsible for taking incoming messages from the socket channel and dispatching them to the proper handler.

Instance Method Summary collapse

Instance Method Details

#dispatch(channel, message) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/volt/tasks/dispatcher.rb', line 5

def dispatch(channel, message)
  callback_id, class_name, method_name, *args = message

  # Get the class
  klass = Object.send(:const_get, class_name)

  if klass.ancestors.include?(TaskHandler)
    # Init and send the method
    begin
      result = klass.new(channel, self).send(method_name, *args)
      error = nil
    rescue => e
      # TODO: Log these errors better
      puts e.inspect
      puts e.backtrace
      result = nil
      error = e
    end

    if callback_id
      # Callback with result
      channel.send_message('response', callback_id, result, error)
    end
  end
end