Skip to content

Instantly share code, notes, and snippets.

@ab
Created October 13, 2011 01:54
Show Gist options
  • Save ab/1283144 to your computer and use it in GitHub Desktop.
Save ab/1283144 to your computer and use it in GitHub Desktop.
A simple test eventmachine server
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
class Server < EM::Connection
def post_init
puts "Someone connected"
send_data "Hello!\n"
end
def receive_data(data)
send_data "Received #{data.length} bytes\n"
if data.strip == 'graceful_stop'
puts 'graceful_stop'
EM.graceful_stop
end
if data.strip == 'stop'
puts 'stop'
EM.stop
end
end
def unbind
puts "Disconnect"
end
end
if $0 == __FILE__
listen = ARGV[0] || '127.0.0.1'
port = 8081
puts "Starting server on #{listen}:#{port}"
EM.run do
EM.start_server listen, 8081, Server
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment