class WebsocketRails::Dispatcher
Deprecation Notices
Attributes
connection_manager[R]
controller_factory[R]
event_map[R]
Public Class Methods
describe_events(&block)
click to toggle source
# File lib/websocket-rails.rb, line 100 def self.describe_events(&block) raise "This method has been deprecated. Please use WebsocketRails::EventMap.describe instead." end
new(connection_manager)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 10 def initialize(connection_manager) @connection_manager = connection_manager @controller_factory = ControllerFactory.new(self) @event_map = EventMap.new(self) end
Public Instance Methods
broadcast_message(event)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 41 def broadcast_message(event) connection_manager.connections.map do |_, connection| connection.trigger event end end
dispatch(event)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 26 def dispatch(event) return if event.is_invalid? if event.is_channel? filter_channel(event) else reload_event_map! unless event.is_internal? route event end end
receive(event_name,data,connection)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 21 def receive(event_name,data,connection) event = Event.new event_name, data, connection dispatch( event ) end
receive_encoded(encoded_data,connection)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 16 def receive_encoded(encoded_data,connection) event = Event.new_from_json( encoded_data, connection ) dispatch( event ) end
reload_event_map!()
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 47 def reload_event_map! return unless defined?(Rails) and !Rails.configuration.cache_classes begin load "#{Rails.root}/config/events.rb" @event_map = EventMap.new(self) rescue Exception => ex log(:warn, "EventMap reload failed: #{ex.message}") end end
send_message(event)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 37 def send_message(event) event.connection.trigger event end
Private Instance Methods
execute(actions)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 105 def execute(actions) actions.map do |action| EM.next_tick { action.resume } end end
extract_exception_data(ex)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 111 def extract_exception_data(ex) if record_invalid_defined? and ex.is_a? ActiveRecord::RecordInvalid { :record => ex.record.attributes, :errors => ex.record.errors, :full_messages => ex.record.errors.full_messages } else ex if ex.respond_to?(:to_json) end end
filter_channel(event)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 79 def filter_channel(event) actions = [] actions << Fiber.new do begin log_event(event) do controller_class, catch_all = filtered_channels[event.channel] controller = controller_factory.new_for_event(event, controller_class, event.name) # send to the method of the event name # silently skip routing to the controller on event.name if it doesnt respond controller.process_action(event.name, event) if controller.respond_to?(event.name) # send to our defined catch all method controller.process_action(catch_all, event) if catch_all end rescue Exception => ex event.success = false event.data = extract_exception_data ex event.trigger end end if filtered_channels[event.channel] actions << Fiber.new{ WebsocketRails[event.channel].trigger_event(event) } execute actions end
record_invalid_defined?()
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 123 def record_invalid_defined? Object.const_defined?('ActiveRecord') and ActiveRecord.const_defined?('RecordInvalid') end
route(event)
click to toggle source
# File lib/websocket_rails/dispatcher.rb, line 59 def route(event) actions = [] event_map.routes_for event do |controller_class, method| actions << Fiber.new do begin log_event(event) do controller = controller_factory.new_for_event(event, controller_class, method) controller.process_action(method, event) end rescue Exception => ex event.success = false event.data = extract_exception_data ex event.trigger end end end execute actions end