//下面程序取自 Poco 庫的Net模塊例子----HTTPServer 下面開始解析代碼 #include "Poco/Net/HTTPServer.h" //繼承自TCPServer 實現了一個完整的HTTP多線程服務器 #include "Poco/Net/HTTPRequestHandler.h" //抽象基類類 被HttpServer所建立 用來處理Http的請求 #include "Poco/Net/HTTPRequestHandlerFactory.h" //HTTPRequestHandler的工廠 給予工廠設計模式 #include "Poco/Net/HTTPServerParams.h" //被用來指定httpserver以及HTTPRequestHandler的參數 #include "Poco/Net/HTTPServerRequest.h" //ServerRequest的抽象子類用來指定 服務器端的 http請求 #include "Poco/Net/HTTPServerResponse.h" //ServerResponse的抽象子類用來指定服務器端的http響應 #include "Poco/Net/ServerSocket.h" //提供了一個TCP服務器套接字接口 #include "Poco/Net/WebSocket.h" //這個類實現了RFC 6455 web套接字接口 專門針對web服務器用 #include "Poco/Net/NetException.h" //網絡異常 #include "Poco/Util/ServerApplication.h" //Application的子類 全部服務器程序 包括 Reactor FTP HTTP等都用到 算是服務器的啓動類 #include "Poco/Util/Option.h" //存儲了命令行選項 #include "Poco/Util/OptionSet.h" //一個Opention對象的集合 #include "Poco/Util/HelpFormatter.h" //從OptionSet格式化幫助信息 #include "Poco/Format.h" //格式化函數的實現相似於 C的 sprintf函數 具體看文檔 #include <iostream> using Poco::Net::ServerSocket; using Poco::Net::WebSocket; using Poco::Net::WebSocketException; using Poco::Net::HTTPRequestHandler; using Poco::Net::HTTPRequestHandlerFactory; using Poco::Net::HTTPServer; using Poco::Net::HTTPServerRequest; using Poco::Net::HTTPResponse; using Poco::Net::HTTPServerResponse; using Poco::Net::HTTPServerParams; using Poco::Timestamp; using Poco::ThreadPool; using Poco::Util::ServerApplication; using Poco::Util::Application; using Poco::Util::Option; using Poco::Util::OptionSet; using Poco::Util::HelpFormatter; //////頁面處理器 連接到來的時候 直接打印html內容 class PageRequestHandler: public HTTPRequestHandler /// Return a HTML document with some JavaScript creating /// a WebSocket connection. { public: void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { response.setChunkedTransferEncoding(true); response.setContentType("text/html"); std::ostream& ostr = response.send(); ostr << "<html>"; ostr << "<head>"; ostr << "<title>WebSocketServer</title>"; ostr << "<script type=\"text/javascript\">"; ostr << "function WebSocketTest()"; ostr << "{"; ostr << " if (\"WebSocket\" in window)"; ostr << " {"; ostr << " var ws = new WebSocket(\"ws://" << request.serverAddress().toString() << "/ws\");"; ostr << " ws.onopen = function()"; ostr << " {"; ostr << " ws.send(\"Hello, world!\");"; ostr << " };"; ostr << " ws.onmessage = function(evt)"; ostr << " { "; ostr << " var msg = evt.data;"; ostr << " alert(\"Message received: \" + msg);"; ostr << " ws.close();"; ostr << " };"; ostr << " ws.onclose = function()"; ostr << " { "; ostr << " alert(\"WebSocket closed.\");"; ostr << " };"; ostr << " }"; ostr << " else"; ostr << " {"; ostr << " alert(\"This browser does not support WebSockets.\");"; ostr << " }"; ostr << "}"; ostr << "</script>"; ostr << "</head>"; ostr << "<body>"; ostr << " <h1>WebSocket Server</h1>"; ostr << " <p><a href=\"javascript:WebSocketTest()\">Run WebSocket Script</a></p>"; ostr << "</body>"; ostr << "</html>"; } }; /////////http請求處理器 1 class WebSocketRequestHandler: public HTTPRequestHandler /// Handle a WebSocket connection. { public: void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { Application& app = Application::instance(); try { WebSocket ws(request, response); app.logger().information("WebSocket connection established."); char buffer[1024]; int flags; int n; do { n = ws.receiveFrame(buffer, sizeof(buffer), flags); app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x).", n, unsigned(flags))); ws.sendFrame(buffer, n, flags); } while (n > 0 || (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE); app.logger().information("WebSocket connection closed."); } catch (WebSocketException& exc) { app.logger().log(exc); switch (exc.code()) { case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION); // fallthrough case WebSocket::WS_ERR_NO_HANDSHAKE: case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); response.setContentLength(0); response.send(); break; } } } }; //HTTP請求處理器工廠 class RequestHandlerFactory: public HTTPRequestHandlerFactory { public: HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) //覆蓋方法 { Application& app = Application::instance(); app.logger().information("Request from " + request.clientAddress().toString() + ": " + request.getMethod() + " " + request.getURI() + " " + request.getVersion());
//打印全部連接請求 for (HTTPServerRequest::ConstIterator it = request.begin(); it != request.end(); ++it) { app.logger().information(it->first + ": " + it->second); } //能夠選擇輸出html頁面 或者不輸出 經過http請求參數 if(request.find("Upgrade") != request.end() && request["Upgrade"] == "websocket") return new WebSocketRequestHandler; else return new PageRequestHandler; } };
//////////網絡應用程序的啓動器 都是ServerApplication class WebSocketServer: public Poco::Util::ServerApplication { public: WebSocketServer(): _helpRequested(false) { } ~WebSocketServer() { } protected:
///再啓動的時候先調用 void initialize(Application& self) { loadConfiguration(); // load default configuration files, if present ServerApplication::initialize(self); } //在釋放的時候調用 void uninitialize() { ServerApplication::uninitialize(); }
///覆蓋基類的函數 定義一些命令選項 void defineOptions(OptionSet& options) { ServerApplication::defineOptions(options); options.addOption( Option("help", "h", "display help information on command line arguments") .required(false) .repeatable(false)); } ///處理命令行選項 void handleOption(const std::string& name, const std::string& value) { ServerApplication::handleOption(name, value); if (name == "help") _helpRequested = true; }
//打印幫助 void displayHelp() { HelpFormatter helpFormatter(options()); helpFormatter.setCommand(commandName()); helpFormatter.setUsage("OPTIONS"); helpFormatter.setHeader("A sample HTTP server supporting the WebSocket protocol."); helpFormatter.format(std::cout); } //在初始化完畢以後調用 int main(const std::vector<std::string>& args) { if (_helpRequested) { displayHelp(); } else { // get parameters from configuration file
//從配置文件中獲取端口 unsigned short port = (unsigned short) config().getInt("WebSocketServer.port", 9980); // 安裝一個ServerSocket ServerSocket svs(port); // s安裝一個HttpServer實例 而且傳遞 請求處理器工廠 和一個HttpServerParams對象 HTTPServer srv(new RequestHandlerFactory, svs, new HTTPServerParams); // 啓動服務器 srv.start(); // 等待kill 或者控制檯CTRL+C waitForTerminationRequest(); // 中止HTTP服務器 srv.stop(); } return Application::EXIT_OK; //返回正常退出狀態 } private: bool _helpRequested; }; //啓動web 服務器 POCO_SERVER_MAIN(WebSocketServer)