diff --git a/Makefile b/Makefile index 4cb1bac..0a8ee66 100644 --- a/Makefile +++ b/Makefile @@ -18,22 +18,8 @@ examples: clang++ -flto -O3 -s *.o -o EchoServer rm *.o -# HttpServer (currently quire broken, mind you) - clang -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c - clang++ -flto -O3 -c -std=c++17 -Isrc -IuSockets/src examples/HttpServer.cpp - clang++ -flto -O3 -s *.o -o HttpServer -lssl -lz -lcrypto -lpthread -lstdc++fs - rm *.o - -# I don't know what this is supposed to do -main: - clang -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c - clang++ -flto -O3 -c -std=c++17 -Isrc -IuSockets/src misc/main.cpp - clang++ -flto -O3 -s *.o -o Main -lssl -lcrypto -lpthread - rm *.o - -# I don't have any tests yet -tests: - clang -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c - clang++ -flto -O3 -c -std=c++17 -Isrc -IuSockets/src tests.cpp - clang++ -flto -O3 -s *.o -o uWS_tests -lssl -lcrypto -lpthread +# EchoServerThreaded (non-SSL, non-Zlib compile) + clang -DLIBUS_NO_SSL -flto -O3 -c -IuSockets/src uSockets/src/*.c uSockets/src/eventing/*.c + clang++ -DLIBUS_NO_SSL -DUWS_NO_ZLIB -flto -O3 -c -std=c++17 -Isrc -IuSockets/src examples/EchoServerThreaded.cpp + clang++ -lpthread -flto -O3 -s *.o -o EchoServerThreaded rm *.o diff --git a/examples/EchoServerThreaded.cpp b/examples/EchoServerThreaded.cpp new file mode 100644 index 0000000..6e4fe4f --- /dev/null +++ b/examples/EchoServerThreaded.cpp @@ -0,0 +1,55 @@ +#include "App.h" +#include +#include + +int main() { + /* ws->getUserData returns one of these */ + struct PerSocketData { + + }; + + /* Simple echo websocket server, using multiple threads */ + std::vector threads(std::thread::hardware_concurrency()); + + std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread *t) { + return new std::thread([]() { + + /* Very simple WebSocket echo server */ + uWS::App().ws("/*", { + /* Settings */ + .compression = uWS::SHARED_COMPRESSOR, + .maxPayloadLength = 16 * 1024, + /* Handlers */ + .open = [](auto *ws, auto *req) { + + }, + .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) { + ws->send(message, opCode); + }, + .drain = [](auto *ws) { + /* Check getBufferedAmount here */ + }, + .ping = [](auto *ws) { + + }, + .pong = [](auto *ws) { + + }, + .close = [](auto *ws, int code, std::string_view message) { + + } + }).listen(9001, [](auto *token) { + if (token) { + std::cout << "Thread " << std::this_thread::get_id() << " listening on port " << 9001 << std::endl; + } else { + std::cout << "Thread " << std::this_thread::get_id() << " failed to listen on port 9001" << std::endl; + } + }).run(); + + }); + }); + + std::for_each(threads.begin(), threads.end(), [](std::thread *t) { + t->join(); + }); +}