From 9cb2fb404fb3787575891e49e1995d42637db01d Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 2 Jun 2020 13:16:10 +0200 Subject: [PATCH] Add UpgradeSync example --- Makefile | 2 +- examples/BroadcastingEchoServer.cpp | 2 +- examples/EchoServer.cpp | 2 +- examples/EchoServerThreaded.cpp | 2 +- examples/UpgradeSync.cpp | 77 +++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 examples/UpgradeSync.cpp diff --git a/Makefile b/Makefile index 230c664..3cdcc81 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -EXAMPLE_FILES := HelloWorld EchoServer BroadcastingEchoServer +EXAMPLE_FILES := HelloWorld EchoServer BroadcastingEchoServer UpgradeSync THREADED_EXAMPLE_FILES := HelloWorldThreaded EchoServerThreaded override CXXFLAGS += -lpthread -Wconversion -std=c++17 -Isrc -IuSockets/src override LDFLAGS += uSockets/*.o -lz diff --git a/examples/BroadcastingEchoServer.cpp b/examples/BroadcastingEchoServer.cpp index d8aee95..8b4fda6 100644 --- a/examples/BroadcastingEchoServer.cpp +++ b/examples/BroadcastingEchoServer.cpp @@ -14,7 +14,7 @@ int main() { .compression = uWS::SHARED_COMPRESSOR, .maxPayloadLength = 16 * 1024 * 1024, .idleTimeout = 10, - .maxBackpressure = 1 * 1024 * 1204, + .maxBackpressure = 1 * 1024 * 1024, /* Handlers */ .open = [](auto *ws) { /* Let's make every connection subscribe to the "broadcast" topic */ diff --git a/examples/EchoServer.cpp b/examples/EchoServer.cpp index 3b73990..80b03f2 100644 --- a/examples/EchoServer.cpp +++ b/examples/EchoServer.cpp @@ -23,7 +23,7 @@ int main() { .compression = uWS::SHARED_COMPRESSOR, .maxPayloadLength = 16 * 1024, .idleTimeout = 10, - .maxBackpressure = 1 * 1024 * 1204, + .maxBackpressure = 1 * 1024 * 1024, /* Handlers */ //.syncUpgrade() diff --git a/examples/EchoServerThreaded.cpp b/examples/EchoServerThreaded.cpp index a3cf9d9..9ae4401 100644 --- a/examples/EchoServerThreaded.cpp +++ b/examples/EchoServerThreaded.cpp @@ -20,7 +20,7 @@ int main() { .compression = uWS::SHARED_COMPRESSOR, .maxPayloadLength = 16 * 1024, .idleTimeout = 10, - .maxBackpressure = 1 * 1024 * 1204, + .maxBackpressure = 1 * 1024 * 1024, /* Handlers */ .open = [](auto *ws) { diff --git a/examples/UpgradeSync.cpp b/examples/UpgradeSync.cpp new file mode 100644 index 0000000..48e9c7f --- /dev/null +++ b/examples/UpgradeSync.cpp @@ -0,0 +1,77 @@ +/* We simply call the root header file "App.h", giving you uWS::App and uWS::SSLApp */ +#include "App.h" + +/* This is a simple WebSocket "sync" upgrade example. + * You may compile it with "WITH_OPENSSL=1 make" or with "make" */ + +int main() { + /* ws->getUserData returns one of these */ + struct PerSocketData { + /* Define your user data */ + int something; + }; + + /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support. + * You may swap to using uWS:App() if you don't need SSL */ + uWS::SSLApp({ + /* There are example certificates in uWebSockets.js repo */ + .key_file_name = "../misc/key.pem", + .cert_file_name = "../misc/cert.pem", + .passphrase = "1234" + }).ws("/*", { + /* Settings */ + .compression = uWS::SHARED_COMPRESSOR, + .maxPayloadLength = 16 * 1024, + .idleTimeout = 10, + .maxBackpressure = 1 * 1024 * 1024, + /* Handlers */ + .upgrade = [](auto *res, auto *req, auto *context) { + + /* You may read from req only here, and COPY whatever you need into your PerSocketData. + * PerSocketData is valid from .open to .close event, accessed with ws->getUserData(). + * HttpRequest (req) is ONLY valid in this very callback, so any data you will need later + * has to be COPIED into PerSocketData here. */ + + /* Immediately upgrading without doing anything "async" before, is simple */ + res->template upgrade({ + /* We initialize PerSocketData struct here */ + .something = 13 + }, req->getHeader("sec-websocket-key"), + req->getHeader("sec-websocket-protocol"), + req->getHeader("sec-websocket-extensions"), + context); + + /* If you don't want to upgrade you can instead respond with custom HTTP here, + * such as res->writeStatus(...)->writeHeader(...)->end(...); or similar.*/ + + /* Performing async upgrade, such as checking with a database is a little more complex; + * see UpgradeAsync example instead. */ + }, + .open = [](auto *ws) { + /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct. + * Here we simply validate that indeed, something == 13 as set in upgrade handler. */ + std::cout << "Something is: " << static_cast(ws->getUserData())->something << std::endl; + }, + .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) { + /* We simply echo whatever data we get */ + ws->send(message, opCode); + }, + .drain = [](auto *ws) { + /* Check ws->getBufferedAmount() here */ + }, + .ping = [](auto *ws) { + /* You don't need to handle this one, we automatically respond to pings as per standard */ + }, + .pong = [](auto *ws) { + /* You don't need to handle this one either */ + }, + .close = [](auto *ws, int code, std::string_view message) { + /* You may access ws->getUserData() here, but sending or + * doing any kind of I/O with the socket is not valid. */ + } + }).listen(9001, [](auto *token) { + if (token) { + std::cout << "Listening on port " << 9001 << std::endl; + } + }).run(); +}