Skip to content

Commit

Permalink
Add chat example
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshi Nakamura committed Aug 19, 2011
1 parent 0163c28 commit 3e3d19a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 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.