Skip to content

Instantly share code, notes, and snippets.

@lcosta
Forked from tmm1/gist:329682
Created March 24, 2010 21:15
Show Gist options
  • Save lcosta/342828 to your computer and use it in GitHub Desktop.
Save lcosta/342828 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'em-http' # gem install em-http-request
require 'yajl' # gem install yajl-ruby
module ChatClient
def post_init
@name = "anonymous_#{ChatClient.client_num+=1}"
@sid = ChatClient.channel.subscribe(method(:send_msg))
@buf = ''
send_data "\033[2;23r"
send_data "\033[2J"
send_data "\033[1H"
header = "Welcome to the EventMachine MWRC Demo (/say, /nick, /quit)".center(80)
send_data "\033[32m#{header}\033[0m"
send_prompt
ChatClient.channel << "\033[1m#{@name}\033[0m has joined.\n"
end
def unbind
ChatClient.channel.unsubscribe(@sid)
ChatClient.channel << "\033[1m#{@name}\033[0m has left.\n"
end
def receive_data data
@buf << data
quit and return if @buf =~ /(\006|\004)/ # ctrl+c or ctrl+d
while line = @buf.slice!(/(.+)\r?\n/)
send_prompt
if line =~ %r|^/nick (.+)|
new_name = $1.strip[0..12]
ChatClient.channel << "\033[1m#{@name}\033[0m is now known as \033[1m#{new_name}\033[0m\n"
@name = new_name
send_prompt
elsif line =~ %r|^/say (.+)|
ChatClient.say_queue.push "#{@name} said #{$1.strip}"
elsif line =~ %r|^/quit|
quit
elsif line =~ /^\e/ # ignore escape sequences
send_prompt
elsif !line.strip.empty?
ChatClient.channel << "\033[1m#{@name}\033[0m: #{line}"
end
end
end
private
def send_msg msg
send_data "\0337"
send_data "\033M"
send_data "\033[23;1H"
send_data msg
send_data "\0338"
end
def send_prompt
send_data "\033[24H"
send_data "\033[2K"
send_data "\033[1m"
send_data @name
send_data "\033[0m: "
end
def quit
send_data "\033[2J"
send_data "\033[1;1H"
header = "Check out http://gist.github.com/329682 for the source to the EM chat server".center(80)
send_data "\033[32m#{header}\033[0m"
send_data "\033[r"
send_data "\033[3;1H"
close_connection_after_writing
end
@client_num = 0
class << self
attr_accessor :client_num
end
def self.channel
@channel ||= EM::Channel.new
end
def self.say_queue
unless @say_queue
@say_queue = EM::Queue.new
# pop off items from the queue one at a time, and pass them to `say`
processor = proc{ |msg|
EM.system("say", msg) do |out, status|
p [msg, out, status]
@say_queue.pop(&processor)
end
}
# pop off the first item
@say_queue.pop(&processor)
end
@say_queue
end
end
EM.run{
EM.start_server '0.0.0.0', 1337, ChatClient
http = EM::HttpRequest.new(
"http://stream.twitter.com/1/statuses/filter.json?track=mwrc,eventmachine"
).get(
:head => {
'authorization' => ['eventmachine', 'eventm4chine']
}
)
parser = Yajl::Parser.new(:symbolize_keys => true)
parser.on_parse_complete = proc{ |item|
name = item[:user][:screen_name]
tweet = item[:text]
p [:twitter, name, tweet]
ChatClient.channel << "\033[34;1m@#{name}\033[0m: #{tweet}\n"
}
http.stream &parser.method(:<<)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment