-
-
Notifications
You must be signed in to change notification settings - Fork 474
add simple signaling-server implemented using libdatachannel cpp-api #1214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tim-S
wants to merge
2
commits into
paullouisageneau:master
Choose a base branch
from
Tim-S:examples/signaling-server-libdatachannel-ws
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you should use |
||
| strcpy(path, argv[0]); | ||
| path[len] = 0; | ||
|
|
||
| char *app_name = NULL; | ||
| // app_name = last_path_segment(path, "\\/"); | ||
| fprintf(stderr, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you should use |
||
| "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 hidden or 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>signaling-server-libdatachannel-ws</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="signaling-server.exe" | ||
| EntryPoint="signaling-server-libdatachannel-ws.App" | ||
| desktop4:Subsystem="console" | ||
| desktop4:SupportsMultipleInstances="true" | ||
| iot2:Subsystem="console" | ||
| iot2:SupportsMultipleInstances="true"> | ||
| <uap:VisualElements | ||
| DisplayName="signaling-server-libdatachannel-ws" | ||
| Description="signaling-server-libdatachannel-ws" | ||
| BackgroundColor="#336699" | ||
| Square150x150Logo="Logo.png" | ||
| Square44x44Logo="SmallLogo44x44.png"> | ||
| <uap:SplashScreen Image="SplashScreen.png" /> | ||
| </uap:VisualElements> | ||
| </Application> | ||
| </Applications> | ||
| </Package> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not mix C functions in C++ when it is not necessary. Use
std::string.