-
-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add examples/signaling-server-libdatachannel-ws
added simple signaling-server implemented using libdatachannel cpp-api
- Loading branch information
Showing
10 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
examples/signaling-server-libdatachannel-ws/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
project(signaling-server-libdatachannel-ws | ||
VERSION 0.1 | ||
LANGUAGES CXX) | ||
|
||
set(MEDIA_UWP_RESOURCES | ||
uwp/Logo.png | ||
uwp/package.appxManifest | ||
uwp/SmallLogo.png | ||
uwp/SmallLogo44x44.png | ||
uwp/SplashScreen.png | ||
uwp/StoreLogo.png | ||
uwp/Windows_TemporaryKey.pfx | ||
) | ||
|
||
if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore") | ||
add_executable(signaling-server-libdatachannel-ws signaling-server.cpp ${MEDIA_UWP_RESOURCES}) | ||
else() | ||
add_executable(signaling-server-libdatachannel-ws signaling-server.cpp) | ||
endif() | ||
|
||
set_target_properties(signaling-server-libdatachannel-ws PROPERTIES | ||
CXX_STANDARD 17 | ||
OUTPUT_NAME signaling-server) | ||
|
||
set_target_properties(signaling-server-libdatachannel-ws PROPERTIES | ||
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER com.github.paullouisageneau.libdatachannel.examples.signaling-server-libdatachannel-ws) | ||
|
||
find_package(Threads REQUIRED) | ||
target_link_libraries(signaling-server-libdatachannel-ws LibDataChannel::LibDataChannel Threads::Threads nlohmann_json::nlohmann_json) | ||
|
||
if(MSVC) | ||
add_custom_command(TARGET signaling-server-libdatachannel-ws POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy_if_different | ||
"$<TARGET_FILE_DIR:datachannel>/datachannel.dll" | ||
$<TARGET_FILE_DIR:signaling-server-libdatachannel-ws> | ||
) | ||
endif() |
162 changes: 162 additions & 0 deletions
162
examples/signaling-server-libdatachannel-ws/signaling-server.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* signaling server example for libdatachannel | ||
* Copyright (c) 2024 Tim Schneider | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include "rtc/rtc.hpp" | ||
|
||
#include <csignal> | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
#include <thread> | ||
#include <unordered_map> | ||
|
||
#include <nlohmann/json.hpp> | ||
using nlohmann::json; | ||
|
||
|
||
using namespace std::chrono_literals; | ||
using std::shared_ptr; | ||
using std::weak_ptr; | ||
template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; } | ||
|
||
std::string get_user(weak_ptr<rtc::WebSocket> wws) { | ||
const auto &ws = wws.lock(); | ||
const auto path_ = ws->path().value(); | ||
const auto user = path_.substr(path_.rfind('/') + 1); | ||
return user; | ||
} | ||
|
||
void signalHandler(int signum) { | ||
std::cout << "Interrupt signal (" << signum << ") received.\n"; | ||
// terminate program | ||
exit(signum); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
rtc::WebSocketServerConfiguration config; | ||
config.port = 8000; | ||
config.enableTls = false; | ||
config.certificatePemFile = std::nullopt; | ||
config.keyPemFile = std::nullopt; | ||
config.keyPemPass = std::nullopt; | ||
config.bindAddress = std::nullopt; | ||
config.connectionTimeout = std::nullopt; | ||
|
||
// Check command line arguments. | ||
for (int i = 1; i < argc; i++) { | ||
if (strcmp(argv[i], "--help") == 0) { | ||
const size_t len = strlen(argv[0]); | ||
char *path = (char *)malloc(len + 1); | ||
strcpy(path, argv[0]); | ||
path[len] = 0; | ||
|
||
char *app_name = NULL; | ||
// app_name = last_path_segment(path, "\\/"); | ||
fprintf(stderr, | ||
"Usage: %s [-p <port>] [-a <bind-address>] [--connection-timeout <timeout>] " | ||
"[--enable-tls] [--certificatePemFile <file>] [--keyPemFile <keyPemFile>] " | ||
"[--keyPemPass <pass>]\n" | ||
"Example:\n" | ||
" %s -p 8000 -a 127.0.0.1 \n", | ||
app_name, app_name); | ||
free(path); | ||
return EXIT_FAILURE; | ||
} | ||
if (strcmp(argv[i], "-p") == 0) { | ||
config.port = atoi(argv[++i]); | ||
continue; | ||
} | ||
if (strcmp(argv[i], "-a") == 0) { | ||
config.bindAddress = argv[++i]; | ||
continue; | ||
} | ||
if (strcmp(argv[i], "--connection-timeout") == 0) { | ||
config.connectionTimeout = std::chrono::milliseconds(atoi(argv[++i])); | ||
continue; | ||
} | ||
if (strcmp(argv[i], "--enable-tls") == 0) { | ||
config.enableTls = true; | ||
continue; | ||
} | ||
if (strcmp(argv[i], "--certificatePemFile") == 0) { | ||
config.certificatePemFile = argv[++i]; | ||
continue; | ||
} | ||
if (strcmp(argv[i], "--keyPemFile") == 0) { | ||
config.keyPemFile = argv[++i]; | ||
continue; | ||
} | ||
if (strcmp(argv[i], "--keyPemPass") == 0) { | ||
config.keyPemPass = argv[++i]; | ||
continue; | ||
} | ||
} | ||
|
||
auto wss = std::make_shared<rtc::WebSocketServer>(config); | ||
std::unordered_map<std::string, std::shared_ptr<rtc::WebSocket>> clients_map; | ||
|
||
wss->onClient([&clients_map](std::shared_ptr<rtc::WebSocket> ws) { | ||
std::promise<void> wsPromise; | ||
auto wsFuture = wsPromise.get_future(); | ||
std::cout << "WebSocket client (remote-address: " << ws->remoteAddress().value() << ")" | ||
<< std::endl; | ||
|
||
ws->onOpen([&clients_map, &wsPromise, wws = make_weak_ptr(ws)]() { | ||
const auto user = get_user(wws); | ||
std::cout << "WebSocket connected (user: " << user << ")" << std::endl; | ||
clients_map.insert_or_assign(user, wws.lock()); | ||
wsPromise.set_value(); | ||
}); | ||
ws->onError([&clients_map, &wsPromise, wws = make_weak_ptr(ws)](std::string s) { | ||
wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s))); | ||
const auto user = get_user(wws); | ||
std::cout << "WebSocket error (user: " << user << ")" << std::endl; | ||
clients_map.erase(user); | ||
}); | ||
ws->onClosed([&clients_map, &wsPromise, wws = make_weak_ptr(ws)]() { | ||
const auto user = get_user(wws); | ||
std::cout << "WebSocket closed (user: " << user << ")" << std::endl; | ||
clients_map.erase(user); | ||
}); | ||
ws->onMessage([&clients_map, wws = make_weak_ptr(ws)](auto data) { | ||
// data holds either std::string or rtc::binary | ||
if (!std::holds_alternative<std::string>(data)) | ||
return; | ||
|
||
json message = json::parse(std::get<std::string>(data)); | ||
|
||
auto it = message.find("id"); | ||
if (it == message.end()) | ||
return; | ||
|
||
auto id = it->get<std::string>(); | ||
|
||
auto client_dst = clients_map.find(id); | ||
if (client_dst == clients_map.end()) { | ||
std::cout << "not found" << std::endl; | ||
} else { | ||
const auto user = get_user(wws); | ||
|
||
message["id"] = user; | ||
auto &[id_dst, ws_dst] = *client_dst; | ||
std::cout << user << "->" << id << ": " << message.dump() << std::endl; | ||
ws_dst->send(message.dump()); | ||
} | ||
}); | ||
std::cout << "Waiting for client to be connected..." << std::endl; | ||
wsFuture.get(); | ||
}); | ||
|
||
signal(SIGINT, signalHandler); | ||
while (true) { | ||
std::this_thread::sleep_for(1s); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.5 KB
examples/signaling-server-libdatachannel-ws/uwp/Windows_TemporaryKey.pfx
Binary file not shown.
42 changes: 42 additions & 0 deletions
42
examples/signaling-server-libdatachannel-ws/uwp/package.appxManifest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Package | ||
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" | ||
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" | ||
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" | ||
xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" | ||
xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2" | ||
IgnorableNamespaces="uap mp"> | ||
|
||
<Identity Name="274EF42C-A3FB-3D6C-B127-E39C508A4F0E" Publisher="CN=CMake" Version="1.0.0.0" /> | ||
<mp:PhoneIdentity PhoneProductId="274EF42C-A3FB-3D6C-B127-E39C508A4F0E" PhonePublisherId="00000000-0000-0000-0000-000000000000"/> | ||
<Properties> | ||
<DisplayName>datachannel-client</DisplayName> | ||
<PublisherDisplayName>CMake</PublisherDisplayName> | ||
<Logo>StoreLogo.png</Logo> | ||
</Properties> | ||
<Dependencies> | ||
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" /> | ||
</Dependencies> | ||
<Resources> | ||
<Resource Language="x-generate" /> | ||
</Resources> | ||
<Applications> | ||
<Application | ||
Id="App" | ||
Executable="client.exe" | ||
EntryPoint="datachannel-client.App" | ||
desktop4:Subsystem="console" | ||
desktop4:SupportsMultipleInstances="true" | ||
iot2:Subsystem="console" | ||
iot2:SupportsMultipleInstances="true"> | ||
<uap:VisualElements | ||
DisplayName="datachannel-client" | ||
Description="datachannel-client" | ||
BackgroundColor="#336699" | ||
Square150x150Logo="Logo.png" | ||
Square44x44Logo="SmallLogo44x44.png"> | ||
<uap:SplashScreen Image="SplashScreen.png" /> | ||
</uap:VisualElements> | ||
</Application> | ||
</Applications> | ||
</Package> |