Class: RocketChat::Realtime::Dispatcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rocket_chat/realtime/dispatcher.rb

Overview

Message Dispatcher

Since:

  • 0.1.0

Constant Summary collapse

HANDLERS =

Since:

  • 0.1.0

{
  # Heartbeat
  'ping' => ->(dispatcher, _) { dispatcher.driver.text({ 'msg': 'pong' }.to_json) },
  # RPC
  'result' => Handlers::Result,
  # Subscription
  'ready' => Handlers::Ready,
  'changed' => Handlers::Changed
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Dispatcher

Returns a new instance of Dispatcher.

Parameters:

  • driver (WebSocket::Driver::Client)

Since:

  • 0.1.0



44
45
46
47
48
# File 'lib/rocket_chat/realtime/dispatcher.rb', line 44

def initialize(client)
  @client = client

  driver.on(:message, &method(:dispatch))
end

Instance Attribute Details

#clientObject (readonly)

Since:

  • 0.1.0



39
40
41
# File 'lib/rocket_chat/realtime/dispatcher.rb', line 39

def client
  @client
end

Instance Method Details

#dispatch(event) ⇒ Boolean

Dispatch messages

Parameters:

  • event (WebSocket::Driver::MessageEvent)

Returns:

  • (Boolean)

    is dispatched

Since:

  • 0.1.0



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rocket_chat/realtime/dispatcher.rb', line 57

def dispatch(event)
  message = JSON.parse(event.data)
  handler = HANDLERS[message.fetch('msg', nil)]
  if handler
    handler.call(self, message)
  else
    logger.debug("No handler found for: #{message}")
    false
  end
rescue JSON::ParserError
  # nope
end

#disposeObject

Dispose

Clear references

Since:

  • 0.1.0



75
76
77
# File 'lib/rocket_chat/realtime/dispatcher.rb', line 75

def dispose
  @client = nil
end