Add threaded websocket server, skip broken examples

This commit is contained in:
Alex Hultman
2019-03-01 18:10:44 +01:00
parent 4fd895c8e7
commit 6801ab8969
2 changed files with 59 additions and 18 deletions
+4 -18
View File
@@ -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
+55
View File
@@ -0,0 +1,55 @@
#include "App.h"
#include <thread>
#include <algorithm>
int main() {
/* ws->getUserData returns one of these */
struct PerSocketData {
};
/* Simple echo websocket server, using multiple threads */
std::vector<std::thread *> 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<PerSocketData>("/*", {
/* 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();
});
}