A
Artūras Šlajus
I wrote my nifty server in GServer, but just stumbled on EventMachine.
Is EM better than my GServer implementation (i guess it is )
class GameServer < GServer
def initialize(*args)
super(*args)
end
def serve(io)
log "Registering #{io} to Dispatcher."
dispatcher = Dispatcher.singleton
dispatcher.register io
loop do
begin
log "Reading from #{io}"
str = nil
Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
until str.nil?
str.strip!
dispatcher.receive io, JSON.load(str)
Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
end
rescue Timeout::Error
end
log "Dispatching outgoing messages..."
dispatcher.each_outgoing_for(io) do |message|
io.write JSON.dump(message)
io.write "\n"
io.flush
end
log "Sleeping..."
sleep CONFIG['sleep_timeout']
end
rescue JSON:arserError
io.write "Unknown protocol, aborting.\n"
rescue Exception => e
log "Exception for #{io}: #{e.inspect}"
end
end
Is EM better than my GServer implementation (i guess it is )
class GameServer < GServer
def initialize(*args)
super(*args)
end
def serve(io)
log "Registering #{io} to Dispatcher."
dispatcher = Dispatcher.singleton
dispatcher.register io
loop do
begin
log "Reading from #{io}"
str = nil
Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
until str.nil?
str.strip!
dispatcher.receive io, JSON.load(str)
Timeout.timeout(CONFIG['read_timeout']) { str = io.gets }
end
rescue Timeout::Error
end
log "Dispatching outgoing messages..."
dispatcher.each_outgoing_for(io) do |message|
io.write JSON.dump(message)
io.write "\n"
io.flush
end
log "Sleeping..."
sleep CONFIG['sleep_timeout']
end
rescue JSON:arserError
io.write "Unknown protocol, aborting.\n"
rescue Exception => e
log "Exception for #{io}: #{e.inspect}"
end
end