Class: Ubiquity::Client
- Inherits:
-
Object
- Object
- Ubiquity::Client
- Defined in:
- lib/ubiquity/protocol.rb
Instance Method Summary collapse
- #exec(&block) ⇒ Object
-
#initialize(remote_ip = 'localhost', port = 1978) ⇒ Client
constructor
A new instance of Client.
- #on(msg, &block) ⇒ Object
Constructor Details
#initialize(remote_ip = 'localhost', port = 1978) ⇒ Client
Returns a new instance of Client.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ubiquity/protocol.rb', line 40 def initialize remote_ip='localhost', port=1978 @hooks = [] @client = TCPSocket.new remote_ip, port Thread.new { while line = @client.gets if line =~ /NOTIFY:(.+)/ notify_msg = $1.gsub("\n", "") @hooks.each do |h| if notify_msg == h[:hook] h[:cb].call end end elsif line =~ /PUTS:(.+)/ puts $1 end end } end |
Instance Method Details
#exec(&block) ⇒ Object
60 61 62 63 |
# File 'lib/ubiquity/protocol.rb', line 60 def exec &block a = block.to_source @client.puts ({:cmd => a}.to_json) end |
#on(msg, &block) ⇒ Object
65 66 67 |
# File 'lib/ubiquity/protocol.rb', line 65 def on msg, &block @hooks << {:hook => msg, :cb => block} end |