Permalink
Browse files

update examples. add interactive keyboard client

  • Loading branch information...
1 parent 8cd48dd commit bbc669597be8a3778d741dceb6898a3976be17cb @jch committed Mar 28, 2012
Showing with 54 additions and 9 deletions.
  1. +9 −0 README.md
  2. +41 −0 examples/interactive.rb
  3. +4 −9 examples/logger.rb
View
@@ -48,6 +48,15 @@ client = EventMachine::IRC::Client.new do |c|
end
````
+## Examples
+
+See `examples` folder for examples. These are directly runnable.
+For example, to run the interactive command line client:
+
+```
+ruby examples/interactive.rb
+```
+
## References
* [API Documentation](http://rubydoc.info/gems/em-irc)
View
@@ -0,0 +1,41 @@
+require File.expand_path '../../lib/em-irc', __FILE__
+require 'logger'
+
+client = EventMachine::IRC::Client.new do
+ host '127.0.0.1'
+ port '6667'
+
+ on :connect do
+ nick 'keybot'
+ join "#general"
+ end
+
+ on :raw do |m|
+ puts "raw message: #{m.inspect}"
+ end
+end
+
+class KeyboardHandler < EM::Connection
+ include EM::Protocols::LineText2
+ attr_reader :queue
+
+ def initialize(q)
+ @queue = q
+ end
+
+ def receive_line(data)
+ @queue.push(data)
+ end
+end
+
+EM.run {
+ q = EM::Queue.new
+ callback = Proc.new do |line|
+ client.instance_eval line
+ q.pop(&callback) # enqueue next pop
+ end
+ q.pop(&callback)
+
+ client.connect
+ EM.open_keyboard(KeyboardHandler, q)
+}
View
@@ -4,21 +4,16 @@
client = EventMachine::IRC::Client.new do
host '127.0.0.1'
port '6667'
- # logger Logger.new(STDOUT)
on :connect do
puts 'client connected'
- join("#general")
+ nick 'em-irc'
+ join "#general"
end
- on :message do |source, target, message|
- puts "#{target} <#{source}> #{message}"
+ on :raw do |m|
+ puts "raw message: #{m.inspect}"
end
-
- # on :raw do |m|
- # # puts "raw message: #{m.inspect}"
- # end
end
-# puts client.callbacks
client.run!

0 comments on commit bbc6695

Please sign in to comment.