Created
October 13, 2011 01:54
-
-
Save ab/1283144 to your computer and use it in GitHub Desktop.
A simple test eventmachine server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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