diff --git a/UnitTest/WebRTCClient/Client.cpp b/UnitTest/WebRTCClient/Client.cpp index 1732ad4..2fbd374 100644 --- a/UnitTest/WebRTCClient/Client.cpp +++ b/UnitTest/WebRTCClient/Client.cpp @@ -46,11 +46,12 @@ public: dc->onClosed([id]() { LOG(info) << "DataChannel from " << id << " closed"; }); - dc->onMessage([id](auto data) { + dc->onMessage([this, id](auto data) { // data holds either std::string or rtc::binary - if (std::holds_alternative(data)) + if (std::holds_alternative(data)) { LOG(info) << "Message from " << id << " received: " << std::get(data); - else + p->log(std::format("{}: {}", id, std::get(data))); + } else LOG(info) << "Binary message from " << id << " received, size=" << std::get(data).size(); }); dataChannelMap.emplace(id, dc); @@ -90,14 +91,16 @@ bool Client::setRemoteId(const std::string &id) { if (auto dc = wdc.lock()) dc->send("Hello from " + m_id); }); m_d->m_sender->onClosed([id]() { LOG(info) << "DataChannel from " << id << " closed"; }); - m_d->m_sender->onMessage([id, wdc = std::weak_ptr(m_d->m_sender)](auto data) { + m_d->m_sender->onMessage([this, id, wdc = std::weak_ptr(m_d->m_sender)](auto data) { // data holds either std::string or rtc::binary - if (std::holds_alternative(data)) + if (std::holds_alternative(data)) { LOG(info) << "Message from " << id << " received: " << std::get(data); - else + log(std::format("{}: {}", id, std::get(data))); + } else LOG(info) << "Binary message from " << id << " received, size=" << std::get(data).size(); }); m_d->dataChannelMap.emplace(id, m_d->m_sender); + ret = true; return ret; } diff --git a/UnitTest/WebRTCClient/main.cpp b/UnitTest/WebRTCClient/main.cpp index b069613..977db9e 100644 --- a/UnitTest/WebRTCClient/main.cpp +++ b/UnitTest/WebRTCClient/main.cpp @@ -16,7 +16,7 @@ ftxui::ButtonOption Style() { return option; } -int main(int argc, char const *argv[]) { +int main(int argc, char const *argv[]) try { Client client; std::string remoteId; std::string message; @@ -25,7 +25,7 @@ int main(int argc, char const *argv[]) { std::string dialogMessga = "请不要不要"; ftxui::InputOption option; option.multiline = false; - auto offerInput = ftxui::Input(remoteId, "remote id", option); + auto offerInput = ftxui::Input(&remoteId, "remote id", option); auto offerButton = ftxui::Button( "Offer", [&]() { @@ -40,8 +40,16 @@ int main(int argc, char const *argv[]) { offerButton, }); - auto messageInput = ftxui::Input(message, "请输入要发送的消息..."); - auto messageButton = ftxui::Button("发送", [&client, &message]() { client.sendMessage(message); }, ftxui::ButtonOption()); + auto messageInput = ftxui::Input(&message, "请输入要发送的消息..."); + auto messageButton = ftxui::Button( + "发送", + [&client, &message]() { + if (!message.empty()) { + client.sendMessage(message); + message.clear(); + } + }, + ftxui::ButtonOption()); auto messageLayout = ftxui::Container::Horizontal({ messageInput, messageButton, @@ -89,4 +97,6 @@ int main(int argc, char const *argv[]) { screen.Loop(component); return 0; +} catch (const std::exception &e) { + std::cout << "Error: " << e.what() << std::endl; } diff --git a/WebApplication/WebRTCClientPage.cpp b/WebApplication/WebRTCClientPage.cpp index 71f74e1..94fbb8f 100644 --- a/WebApplication/WebRTCClientPage.cpp +++ b/WebApplication/WebRTCClientPage.cpp @@ -39,11 +39,12 @@ WebRTCClientPage::WebRTCClientPage() : Wt::WTemplate(tr("Wt.WebRTC.Home")) { sendBtn->setText("发送"); sendBtn->setDisabled(true); + // clang-format off // std::string url = "ws://127.0.0.1:8081/api/v1/webrtc/signal"; - std::string url = std::format("ws://{}/api/v1/webrtc/signal", app->environment().hostName()); + std::string url = std::format("{}://{}/api/v1/webrtc/signal", app->environment().urlScheme() == "https" ? "wss" : "ws", app->environment().hostName()); setJavaScriptMember(" WebRTCClient", std::format("new {}.WebRTCClient({},{},{},{},{},{},{}, '{}', '{}');", WT_CLASS, WT_CLASS, - jsRef(), offerId->jsRef(), offerBtn->jsRef(), sendMsg->jsRef(), - sendBtn->jsRef(), textBrowser->jsRef(), localId, url)); + jsRef(), offerId->jsRef(), offerBtn->jsRef(), sendMsg->jsRef(),sendBtn->jsRef(), textBrowser->jsRef(), localId, url)); + // clang-format on } std::string WebRTCClientPage::randomId(size_t length) {