| 1 | // |
| 2 | // WebSocketServer.cpp |
| 3 | // |
| 4 | // This sample demonstrates the WebSocket class. |
| 5 | // |
| 6 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/Net/HTTPServer.h" |
| 14 | #include "Poco/Net/HTTPRequestHandler.h" |
| 15 | #include "Poco/Net/HTTPRequestHandlerFactory.h" |
| 16 | #include "Poco/Net/HTTPServerParams.h" |
| 17 | #include "Poco/Net/HTTPServerRequest.h" |
| 18 | #include "Poco/Net/HTTPServerResponse.h" |
| 19 | #include "Poco/Net/HTTPServerParams.h" |
| 20 | #include "Poco/Net/ServerSocket.h" |
| 21 | #include "Poco/Net/WebSocket.h" |
| 22 | #include "Poco/Net/NetException.h" |
| 23 | #include "Poco/Util/ServerApplication.h" |
| 24 | #include "Poco/Util/Option.h" |
| 25 | #include "Poco/Util/OptionSet.h" |
| 26 | #include "Poco/Util/HelpFormatter.h" |
| 27 | #include "Poco/Format.h" |
| 28 | #include <iostream> |
| 29 | |
| 30 | |
| 31 | using Poco::Net::ServerSocket; |
| 32 | using Poco::Net::WebSocket; |
| 33 | using Poco::Net::WebSocketException; |
| 34 | using Poco::Net::HTTPRequestHandler; |
| 35 | using Poco::Net::HTTPRequestHandlerFactory; |
| 36 | using Poco::Net::HTTPServer; |
| 37 | using Poco::Net::HTTPServerRequest; |
| 38 | using Poco::Net::HTTPResponse; |
| 39 | using Poco::Net::HTTPServerResponse; |
| 40 | using Poco::Net::HTTPServerParams; |
| 41 | using Poco::Timestamp; |
| 42 | using Poco::ThreadPool; |
| 43 | using Poco::Util::ServerApplication; |
| 44 | using Poco::Util::Application; |
| 45 | using Poco::Util::Option; |
| 46 | using Poco::Util::OptionSet; |
| 47 | using Poco::Util::HelpFormatter; |
| 48 | |
| 49 | |
| 50 | class PageRequestHandler: public HTTPRequestHandler |
| 51 | /// Return a HTML document with some JavaScript creating |
| 52 | /// a WebSocket connection. |
| 53 | { |
| 54 | public: |
| 55 | void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) |
| 56 | { |
| 57 | response.setChunkedTransferEncoding(true); |
| 58 | response.setContentType("text/html" ); |
| 59 | std::ostream& ostr = response.send(); |
| 60 | ostr << "<html>" ; |
| 61 | ostr << "<head>" ; |
| 62 | ostr << "<title>WebSocketServer</title>" ; |
| 63 | ostr << "<script type=\"text/javascript\">" ; |
| 64 | ostr << "function WebSocketTest()" ; |
| 65 | ostr << "{" ; |
| 66 | ostr << " if (\"WebSocket\" in window)" ; |
| 67 | ostr << " {" ; |
| 68 | ostr << " var ws = new WebSocket(\"ws://" << request.serverAddress().toString() << "/ws\");" ; |
| 69 | ostr << " ws.onopen = function()" ; |
| 70 | ostr << " {" ; |
| 71 | ostr << " ws.send(\"Hello, world!\");" ; |
| 72 | ostr << " };" ; |
| 73 | ostr << " ws.onmessage = function(evt)" ; |
| 74 | ostr << " { " ; |
| 75 | ostr << " var msg = evt.data;" ; |
| 76 | ostr << " alert(\"Message received: \" + msg);" ; |
| 77 | ostr << " ws.close();" ; |
| 78 | ostr << " };" ; |
| 79 | ostr << " ws.onclose = function()" ; |
| 80 | ostr << " { " ; |
| 81 | ostr << " alert(\"WebSocket closed.\");" ; |
| 82 | ostr << " };" ; |
| 83 | ostr << " }" ; |
| 84 | ostr << " else" ; |
| 85 | ostr << " {" ; |
| 86 | ostr << " alert(\"This browser does not support WebSockets.\");" ; |
| 87 | ostr << " }" ; |
| 88 | ostr << "}" ; |
| 89 | ostr << "</script>" ; |
| 90 | ostr << "</head>" ; |
| 91 | ostr << "<body>" ; |
| 92 | ostr << " <h1>WebSocket Server</h1>" ; |
| 93 | ostr << " <p><a href=\"javascript:WebSocketTest()\">Run WebSocket Script</a></p>" ; |
| 94 | ostr << "</body>" ; |
| 95 | ostr << "</html>" ; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | class WebSocketRequestHandler: public HTTPRequestHandler |
| 101 | /// Handle a WebSocket connection. |
| 102 | { |
| 103 | public: |
| 104 | void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) |
| 105 | { |
| 106 | Application& app = Application::instance(); |
| 107 | try |
| 108 | { |
| 109 | WebSocket ws(request, response); |
| 110 | app.logger().information("WebSocket connection established." ); |
| 111 | char buffer[1024]; |
| 112 | int flags; |
| 113 | int n; |
| 114 | do |
| 115 | { |
| 116 | n = ws.receiveFrame(buffer, sizeof(buffer), flags); |
| 117 | app.logger().information(Poco::format("Frame received (length=%d, flags=0x%x)." , n, unsigned(flags))); |
| 118 | ws.sendFrame(buffer, n, flags); |
| 119 | } |
| 120 | while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE); |
| 121 | app.logger().information("WebSocket connection closed." ); |
| 122 | } |
| 123 | catch (WebSocketException& exc) |
| 124 | { |
| 125 | app.logger().log(exc); |
| 126 | switch (exc.code()) |
| 127 | { |
| 128 | case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION: |
| 129 | response.set("Sec-WebSocket-Version" , WebSocket::WEBSOCKET_VERSION); |
| 130 | // fallthrough |
| 131 | case WebSocket::WS_ERR_NO_HANDSHAKE: |
| 132 | case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION: |
| 133 | case WebSocket::WS_ERR_HANDSHAKE_NO_KEY: |
| 134 | response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST); |
| 135 | response.setContentLength(0); |
| 136 | response.send(); |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | class RequestHandlerFactory: public HTTPRequestHandlerFactory |
| 145 | { |
| 146 | public: |
| 147 | HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) |
| 148 | { |
| 149 | Application& app = Application::instance(); |
| 150 | app.logger().information("Request from " |
| 151 | + request.clientAddress().toString() |
| 152 | + ": " |
| 153 | + request.getMethod() |
| 154 | + " " |
| 155 | + request.getURI() |
| 156 | + " " |
| 157 | + request.getVersion()); |
| 158 | |
| 159 | for (HTTPServerRequest::ConstIterator it = request.begin(); it != request.end(); ++it) |
| 160 | { |
| 161 | app.logger().information(it->first + ": " + it->second); |
| 162 | } |
| 163 | |
| 164 | if(request.find("Upgrade" ) != request.end() && Poco::icompare(request["Upgrade" ], "websocket" ) == 0) |
| 165 | return new WebSocketRequestHandler; |
| 166 | else |
| 167 | return new PageRequestHandler; |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | class WebSocketServer: public Poco::Util::ServerApplication |
| 173 | /// The main application class. |
| 174 | /// |
| 175 | /// This class handles command-line arguments and |
| 176 | /// configuration files. |
| 177 | /// Start the WebSocketServer executable with the help |
| 178 | /// option (/help on Windows, --help on Unix) for |
| 179 | /// the available command line options. |
| 180 | /// |
| 181 | /// To use the sample configuration file (WebSocketServer.properties), |
| 182 | /// copy the file to the directory where the WebSocketServer executable |
| 183 | /// resides. If you start the debug version of the WebSocketServer |
| 184 | /// (WebSocketServerd[.exe]), you must also create a copy of the configuration |
| 185 | /// file named WebSocketServerd.properties. In the configuration file, you |
| 186 | /// can specify the port on which the server is listening (default |
| 187 | /// 9980) and the format of the date/time string sent back to the client. |
| 188 | /// |
| 189 | /// To test the WebSocketServer you can use any web browser (http://localhost:9980/). |
| 190 | { |
| 191 | public: |
| 192 | WebSocketServer(): _helpRequested(false) |
| 193 | { |
| 194 | } |
| 195 | |
| 196 | ~WebSocketServer() |
| 197 | { |
| 198 | } |
| 199 | |
| 200 | protected: |
| 201 | void initialize(Application& self) |
| 202 | { |
| 203 | loadConfiguration(); // load default configuration files, if present |
| 204 | ServerApplication::initialize(self); |
| 205 | } |
| 206 | |
| 207 | void uninitialize() |
| 208 | { |
| 209 | ServerApplication::uninitialize(); |
| 210 | } |
| 211 | |
| 212 | void defineOptions(OptionSet& options) |
| 213 | { |
| 214 | ServerApplication::defineOptions(options); |
| 215 | |
| 216 | options.addOption( |
| 217 | Option("help" , "h" , "display help information on command line arguments" ) |
| 218 | .required(false) |
| 219 | .repeatable(false)); |
| 220 | } |
| 221 | |
| 222 | void handleOption(const std::string& name, const std::string& value) |
| 223 | { |
| 224 | ServerApplication::handleOption(name, value); |
| 225 | |
| 226 | if (name == "help" ) |
| 227 | _helpRequested = true; |
| 228 | } |
| 229 | |
| 230 | void displayHelp() |
| 231 | { |
| 232 | HelpFormatter helpFormatter(options()); |
| 233 | helpFormatter.setCommand(commandName()); |
| 234 | helpFormatter.setUsage("OPTIONS" ); |
| 235 | helpFormatter.setHeader("A sample HTTP server supporting the WebSocket protocol." ); |
| 236 | helpFormatter.format(std::cout); |
| 237 | } |
| 238 | |
| 239 | int main(const std::vector<std::string>& args) |
| 240 | { |
| 241 | if (_helpRequested) |
| 242 | { |
| 243 | displayHelp(); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | // get parameters from configuration file |
| 248 | unsigned short port = (unsigned short) config().getInt("WebSocketServer.port" , 9980); |
| 249 | |
| 250 | // set-up a server socket |
| 251 | ServerSocket svs(port); |
| 252 | // set-up a HTTPServer instance |
| 253 | HTTPServer srv(new RequestHandlerFactory, svs, new HTTPServerParams); |
| 254 | // start the HTTPServer |
| 255 | srv.start(); |
| 256 | // wait for CTRL-C or kill |
| 257 | waitForTerminationRequest(); |
| 258 | // Stop the HTTPServer |
| 259 | srv.stop(); |
| 260 | } |
| 261 | return Application::EXIT_OK; |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | bool _helpRequested; |
| 266 | }; |
| 267 | |
| 268 | |
| 269 | POCO_SERVER_MAIN(WebSocketServer) |
| 270 | |