Skip to content

Instantly share code, notes, and snippets.

@dansimpson
Created September 1, 2010 18:44
Show Gist options
  • Save dansimpson/561141 to your computer and use it in GitHub Desktop.
Save dansimpson/561141 to your computer and use it in GitHub Desktop.
require "rubygems"
require "eventmachine"
class HttpHandler < EM::Connection
def initialize *args
super
@header = ""
@status = 200
end
def receive_data data
@request = data
on_request
end
def on_request
end
def set_header field, value
@header << "#{field}: #{value}\r\n"
end
def respond data
send_data "HTTP/1.1 #{@status}\r\n"
send_data @header
send_data "\r\n"
send_data data
close_connection_after_writing
end
end
class MyHttpServer < HttpHandler
def on_request
set_header "Content-Type", "text/plain"
respond "Hello World\n"
end
end
EM.set_descriptor_table_size 8192
EM.epoll = true if EM.epoll?
EM.kqueue = true if EM.kqueue?
EM.run do
EM.start_server "0.0.0.0", 8124, MyHttpServer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment