box-o-sand/ruby-sockets/marco-polo/em-server.rb

34 lines
749 B
Ruby
Raw Normal View History

require 'logger'
require 'eventmachine'
require 'sequel'
DB = Sequel.connect("postgres://#{ENV['USER']}@localhost/marco_polo",
:max_connections => 10)
DB.create_table! :locations do
primary_key :id
String :client
Integer :x
Integer :y
end
module MarcoPoloServer
def post_init
@locations = DB[:locations]
end
def receive_data(data)
client, x, y = data.split
@locations.insert(:client => client, :x => x.to_i, :y => y.to_i)
send_data("thanks\n")
rescue => e
STDERR.puts("#{e.class.name} #{e.message}: #{e.backtrace.join("\n")}")
end
end
EventMachine.run do
host, port = '0.0.0.0', 22000
EventMachine.start_server(host, port, MarcoPoloServer)
puts "Listening on #{host}:#{port}"
end