making basic server work with dependencies for uwebsockets added to cmake

This commit is contained in:
talksik
2024-01-27 11:42:27 -08:00
parent cbe57d4c18
commit 5abd11124d
5 changed files with 98 additions and 190 deletions
+5
View File
@@ -4,6 +4,9 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(HomechatServer)
find_library(USOCKETS_LIBRARY uSockets.a PATHS /usr/local/lib REQUIRED)
find_package(ZLIB REQUIRED)
set(source_dir ${CMAKE_CURRENT_SOURCE_DIR}/src)
file(GLOB source_files
${source_dir}/*.cpp
@@ -11,3 +14,5 @@ file(GLOB source_files
add_executable(${PROJECT_NAME} ${source_files})
target_include_directories(${PROJECT_NAME} PUBLIC ${source_dir}/include)
target_link_libraries(${PROJECT_NAME} ${USOCKETS_LIBRARY})
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
+7
View File
@@ -0,0 +1,7 @@
[
{
"directory": "/home/talksik/code/homechat/server/build",
"command": "/usr/bin/c++ -I/home/talksik/code/homechat/server/src/include -g -o CMakeFiles/HomechatServer.dir/src/Main.cpp.o -c /home/talksik/code/homechat/server/src/Main.cpp",
"file": "/home/talksik/code/homechat/server/src/Main.cpp"
}
]
+67 -1
View File
@@ -1,7 +1,73 @@
#include <iostream>
#include <uWebSockets/App.h>
int main()
{
std::cout << "Hello World!\n";
std::cout << "Welcome to the homechat server!\n";
/* ws->getUserData returns one of these */
struct PerSocketData
{
/* Fill with user data */
};
/* 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::App({/* There are example certificates in uWebSockets.js repo */
.key_file_name = "misc/key.pem",
.cert_file_name = "misc/cert.pem",
.passphrase = "1234"})
.ws<PerSocketData>(
"/*", {/* Settings */
.compression = uWS::CompressOptions(uWS::DEDICATED_COMPRESSOR_4KB | uWS::DEDICATED_DECOMPRESSOR),
.maxPayloadLength = 100 * 1024 * 1024,
.idleTimeout = 16,
.maxBackpressure = 100 * 1024 * 1024,
.closeOnBackpressureLimit = false,
.resetIdleTimeoutOnSend = false,
.sendPingsAutomatically = true,
/* Handlers */
.upgrade = nullptr,
.open =
[](auto * /*ws*/) {
/* Open event here, you may access ws->getUserData() which points to a PerSocketData struct
*/
},
.message =
[](auto *ws, std::string_view message, uWS::OpCode opCode) {
/* This is the opposite of what you probably want; compress if message is LARGER than 16 kb
* the reason we do the opposite here; compress if SMALLER than 16 kb is to allow for
* benchmarking of large message sending without compression */
ws->send(message, opCode, message.length() < 16 * 1024);
},
.dropped =
[](auto * /*ws*/, std::string_view /*message*/, uWS::OpCode /*opCode*/) {
/* A message was dropped due to set maxBackpressure and closeOnBackpressureLimit limit */
},
.drain =
[](auto * /*ws*/) {
/* Check ws->getBufferedAmount() here */
},
.ping =
[](auto * /*ws*/, std::string_view) {
/* Not implemented yet */
},
.pong =
[](auto * /*ws*/, std::string_view) {
/* Not implemented yet */
},
.close =
[](auto * /*ws*/, int /*code*/, std::string_view /*message*/) {
/* You may access ws->getUserData() here */
}})
.listen(9001,
[](auto *listen_socket) {
if (listen_socket)
{
std::cout << "Listening on port " << 9001 << std::endl;
}
})
.run();
return 0;
}