2013-05-28 176 views
0

我试图启动eventmachine httpserver示例,但我在process_http_request方法中添加了简单的puts。令我惊讶的是,当我从浏览器访问本地主机:8080时,我在终端中输出了两次putsEventmachine调用回调两次

为什么打印两次?这是一个错误吗?也许我误解了eventmachine中的某些东西。 你可以在下面看到我的例子。

require 'eventmachine' 
require 'evma_httpserver' 

class MyHttpServer < EM::Connection 
    include EM::HttpServer 

    def post_init 
    super 
    no_environment_strings 
    end 

    def process_http_request 
    response = EM::DelegatedHttpResponse.new(self) 
    response.status = 200 
    response.content_type 'text/html' 
    response.content = '<center><h1>Hi there</h1></center>' 
    puts 'my_test_string' 
    response.send_response 
    end 
end 

EM.run do 
    EM.start_server '0.0.0.0', 8080, MyHttpServer 
end 

回答

1

第一个是对favicon的请求。第二个是对页面主体的请求。如果你想把它称为一个错误,这是你的错误,而不是图书馆的错误。

+0

哦,我应该打印出收到的数据,然后我会看到“get /favicon.ico”,但是非常感谢你:) –