Class: XRBP::WebSocket::Plugins::CommandDispatcher

Inherits:
MessageDispatcher show all
Defined in:
lib/xrbp/websocket/plugins/command_dispatcher.rb

Overview

Dispatch commands (based on message dispatcher)

Examples:

dispatching server info command

connection = WebSocket::Connection.new "wss://s1.ripple.com:443"
connection.add_plugin :command_dispatcher
puts connection.cmd(WebSocket::Cmds::ServerInfo.new)

Constant Summary

Constants inherited from MessageDispatcher

MessageDispatcher::DEFAULT_TIMEOUT

Instance Attribute Summary

Attributes inherited from MessageDispatcher

#message_timeout, #messages

Attributes inherited from PluginBase

#connection

Instance Method Summary collapse

Methods inherited from MessageDispatcher

#cancel_all_messages, #cancel_message, #closed, #initialize, #message, #opened, #parsing_plugins, #try_next, #unlock!

Methods inherited from PluginBase

#initialize

Constructor Details

This class inherits a constructor from XRBP::WebSocket::Plugins::MessageDispatcher

Instance Method Details

#addedObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/xrbp/websocket/plugins/command_dispatcher.rb', line 11

def added
  super

  plugin = self

  connection.define_instance_method(:cmd) do |cmd, &bl|
    return next_connection.cmd cmd, &bl if self.kind_of?(MultiConnection)

    cmd = Command.new(cmd) unless cmd.kind_of?(Command)
    msg(cmd, &bl)
  end
end

#match_message(msg) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xrbp/websocket/plugins/command_dispatcher.rb', line 24

def match_message(msg)
  begin
    return nil if msg.data == ""
    parsed = JSON.parse(msg.data)

  rescue => e
    return nil
  end

  id = parsed['id']
  msg = messages.find { |msg| msg.kind_of?(Command) && msg.id == id }

  return nil unless msg
  [msg, parsed]
end