box-o-sand/ruby-sockets/marco-polo/em-server.rb
Dan Buch d67d1632a8 Throwing three non-pausing clients at mysql-backed EM server
is causing intermittent port binding problems which
manifest as an unreachable server.  Inserting a pause
on the client side fixes the problem.  Hm.
2012-06-16 01:07:53 -04:00

40 lines
805 B
Ruby

require 'eventmachine'
require 'sequel'
if ENV['MPDB']
DB = Sequel.connect(ENV['MPDB'])
else
DB = Sequel.connect('sqlite://marco-polo.sqlite3')
end
begin
DB[:locations].empty?
rescue Sequel::DatabaseError
DB.create_table(:locations) do
primary_key :id
String :client
Integer :x
Integer :y
end
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