2013-05-28 229 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」,但是非常感謝你:) –