Skip to content
Browse files

Add chat example

  • Loading branch information...
1 parent 0163c28 commit 3e3d19ae8203208a37f509818c095fa5fa2d4d81 @nahi committed Aug 20, 2011
Showing with 31 additions and 0 deletions.
  1. +31 −0 chat.rb
View
31 chat.rb
@@ -0,0 +1,31 @@
+# Original code is at https://gist.github.com/948374
+module ChatClient
+ def self.channel
+ @channel ||= EM::Channel.new
+ end
+
+ def post_init
+ @buf = ''
+ @name = "anonymous_#{ChatClient.client_num+=1}"
+ @sid = ChatClient.channel.subscribe { |msg|
+ send_msg(msg)
+ }
+ # ...
+ ChatClient.channel << "#{@name.bold} has joined.\n"
+ end
+
+ def unbind
+ ChatClient.channel.unsubscribe(@sid)
+ ChatClient.channel << "#{@name.bold} has left.\n"
+ end
+
+ def receive_data data
+ @buf << data
+ # ...
+ while line = extract_line(@buf)
+ ChatClient.channel << "#{@name.bold}: #{line}\n"
+ end
+ end
+
+ # ...
+end

0 comments on commit 3e3d19a

Please sign in to comment.
Something went wrong with that request. Please try again.