Ticket #31: httpserver.rb

File httpserver.rb, 0.6 kB (added by tmm1, 1 year ago)
Line 
1 require 'eventmachine'
2 require 'evma_httpserver'
3 require 'delegate'
4
5 class MyHttpResponse < EM::HttpResponse
6   extend Forwardable
7   def_delegators :@connection, :send_data, :close_connection_after_writing, :close_connection
8
9   def initialize connection
10     @connection = connection
11     super()
12   end
13 end
14
15 class MyHttpServer < EM::Connection
16   include EM::HttpServer
17
18   def process_http_request
19     response = MyHttpResponse.new(self)
20     response.status = 200
21     response.content_type 'text/html'
22     response.content = '<center><h1>Hi there</h1></center>'
23     response.send_response
24   end
25 end
26
27 # EM.kqueue
28 EM.run{
29   EM.start_server '0.0.0.0', 8080, MyHttpServer
30 }