Share
Embed
Transcript
25 slides
-
EventMachine Chris Gaffney Collective Idea Ruby::AZ January 10, 2011 Making
networking not suck -
require "socket" server = TCPServer.new("0.0.0.0", 1845) loop do client =
server.accept while line = client.gets client.write(line) end end -
require "socket" server = TCPServer.new("0.0.0.0", 1845) loop do client =
server.accept while line = client.gets client.write(line) end end -
require "socket" server = TCPServer.new("0.0.0.0", 1845) loop do client =
server.accept Thread.new do while line = client.gets client.write(line) end end end -
server = TCPServer.new(1845) clients = [] loop do ready =
IO.select([server] + clients) ready[0].each do |socket| if socket == server clients << socket.accept_nonblock else data = socket.recv_nonblock(256) if data.size > 0 socket.sendmsg_nonblock(data) else clients.delete(socket) end end end end -
require 'rubygems' require 'eventmachine' module Server def receive_data(data) send_data(data) end
end EventMachine.run do EventMachine.start_server('0.0.0.0', 1845, Server) end -
class Server < EventMachine::Connection # def post_init # def receive_data(data)
# def unbind end EventMachine.run do EventMachine.start_server('127.0.0.1', 1845, Server) end -
class Client < EventMachine::Connection # def post_init # def connection_completed
# def receive_data(data) # def unbind end EventMachine.run do EventMachine.connect('localhost', 1845, Client) EventMachine.connect('localhost', 1845, Client) end -
EventMachine.add_periodic_timer(5) do send_ping end EventMachine.add_timer(30) do close_connection unless response end
timer = EventMachine::PeriodicTimer.new(60) do run_crons end # timer.cancel -
EventMachine .add_timer(delay, &block) .add_periodic_timer(delay, &block) .connect(bind, port, handler, *args) .start_server(bind,
port, handler, *args) .run(&block) EventMachine::Connection #send_data(data) #close_connection(after_writing = false) #reconnect(server, port) Rdocs: http://rubyeventmachine.com/ -
em-http-request EventMachine.run do query = { :lat => '37.704276', :lng
=> '-85.871227', :radius => 50 } head = { :accept => 'application/json' } http = EventMachine::HttpRequest.new("http://api.gowalla.com/spots"). get(:query => query, :head => head) http.callback do data = Yajl::Parser.parse(http.response) data['spots'].each do |spot| # American Legion Public Pool @ 37.705699083:-85.8540598 puts "#{spot['name']} @ #{spot['lat']}:#{spot['lng']}" end end end -
em-mongo EventMachine.run do mongo = EM::Mongo::Connection.new.db("demos") tweets = mongo.collection("tweets") tweets.insert({
:user => 'gaffneyc', :text => 'Hello from Ruby::AZ'}) tweets.find(:user => 'gaffneyc') do |results| # [{ # "_id" => BSON::ObjectId('4d2a0037f50f833b19000001'), # "user" => "gaffneyc", # "text" => "Hello from Ruby::AZ" # }] end # tweets.remove(id) # tweets.update(conditions, updates) end -
em-redis EventMachine.run do redis = EM::Protocols::Redis.connect redis.set("test", "5000") do |_|
redis.get("test") do |result| # "5000" puts result.inspect redis.delete("test") end end end Protip: Resque.redis = EM::Protocols::Redis.connect -
twitter-stream EventMachine.run do stream = ::Twitter::JSONStream.connect( :filters => "ruby,rails,collectiveidea", :auth
=> "[username:password]" ) stream.each_item do |item| tweet = Yajl::Parser.parse(item) # {"text"=>"So.. I just found out Justin Bieber's in the...} puts tweet.inspect end # stream.on_error # stream.on_max_reconnects end -
‣ amqp https://github.com/tmm1/amqp/ ‣ em-websocket https://github.com/igrigorik/em-websocket ‣ em-spec https://github.com/joshbuddy/em-spec ‣
em-synchrony https://github.com/igrigorik/em-synchrony -
‣ em-http-request https://github.com/igrigorik/em-http-request ‣ em-mongo https://github.com/bcg/em-mongo ‣ em-redis https://github.com/libc/em-redis ‣
twitter-stream https://github.com/voloko/twitter-stream