add wss.
All checks were successful
Deploy / Build (push) Successful in 6m11s

This commit is contained in:
amass 2025-01-14 18:02:12 +08:00
parent 94c246aa7b
commit 9e8e847787
3 changed files with 27 additions and 13 deletions

View File

@ -46,11 +46,12 @@ public:
dc->onClosed([id]() { LOG(info) << "DataChannel from " << id << " closed"; }); 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 // data holds either std::string or rtc::binary
if (std::holds_alternative<std::string>(data)) if (std::holds_alternative<std::string>(data)) {
LOG(info) << "Message from " << id << " received: " << std::get<std::string>(data); LOG(info) << "Message from " << id << " received: " << std::get<std::string>(data);
else p->log(std::format("{}: {}", id, std::get<std::string>(data)));
} else
LOG(info) << "Binary message from " << id << " received, size=" << std::get<rtc::binary>(data).size(); LOG(info) << "Binary message from " << id << " received, size=" << std::get<rtc::binary>(data).size();
}); });
dataChannelMap.emplace(id, dc); 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); 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->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 // data holds either std::string or rtc::binary
if (std::holds_alternative<std::string>(data)) if (std::holds_alternative<std::string>(data)) {
LOG(info) << "Message from " << id << " received: " << std::get<std::string>(data); LOG(info) << "Message from " << id << " received: " << std::get<std::string>(data);
else log(std::format("{}: {}", id, std::get<std::string>(data)));
} else
LOG(info) << "Binary message from " << id << " received, size=" << std::get<rtc::binary>(data).size(); LOG(info) << "Binary message from " << id << " received, size=" << std::get<rtc::binary>(data).size();
}); });
m_d->dataChannelMap.emplace(id, m_d->m_sender); m_d->dataChannelMap.emplace(id, m_d->m_sender);
ret = true;
return ret; return ret;
} }

View File

@ -16,7 +16,7 @@ ftxui::ButtonOption Style() {
return option; return option;
} }
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) try {
Client client; Client client;
std::string remoteId; std::string remoteId;
std::string message; std::string message;
@ -25,7 +25,7 @@ int main(int argc, char const *argv[]) {
std::string dialogMessga = "请不要不要"; std::string dialogMessga = "请不要不要";
ftxui::InputOption option; ftxui::InputOption option;
option.multiline = false; option.multiline = false;
auto offerInput = ftxui::Input(remoteId, "remote id", option); auto offerInput = ftxui::Input(&remoteId, "remote id", option);
auto offerButton = ftxui::Button( auto offerButton = ftxui::Button(
"Offer", "Offer",
[&]() { [&]() {
@ -40,8 +40,16 @@ int main(int argc, char const *argv[]) {
offerButton, offerButton,
}); });
auto messageInput = ftxui::Input(message, "请输入要发送的消息..."); auto messageInput = ftxui::Input(&message, "请输入要发送的消息...");
auto messageButton = ftxui::Button("发送", [&client, &message]() { client.sendMessage(message); }, ftxui::ButtonOption()); auto messageButton = ftxui::Button(
"发送",
[&client, &message]() {
if (!message.empty()) {
client.sendMessage(message);
message.clear();
}
},
ftxui::ButtonOption());
auto messageLayout = ftxui::Container::Horizontal({ auto messageLayout = ftxui::Container::Horizontal({
messageInput, messageInput,
messageButton, messageButton,
@ -89,4 +97,6 @@ int main(int argc, char const *argv[]) {
screen.Loop(component); screen.Loop(component);
return 0; return 0;
} catch (const std::exception &e) {
std::cout << "Error: " << e.what() << std::endl;
} }

View File

@ -39,11 +39,12 @@ WebRTCClientPage::WebRTCClientPage() : Wt::WTemplate(tr("Wt.WebRTC.Home")) {
sendBtn->setText("发送"); sendBtn->setText("发送");
sendBtn->setDisabled(true); sendBtn->setDisabled(true);
// clang-format off
// std::string url = "ws://127.0.0.1:8081/api/v1/webrtc/signal"; // 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, setJavaScriptMember(" WebRTCClient", std::format("new {}.WebRTCClient({},{},{},{},{},{},{}, '{}', '{}');", WT_CLASS, WT_CLASS,
jsRef(), offerId->jsRef(), offerBtn->jsRef(), sendMsg->jsRef(), jsRef(), offerId->jsRef(), offerBtn->jsRef(), sendMsg->jsRef(),sendBtn->jsRef(), textBrowser->jsRef(), localId, url));
sendBtn->jsRef(), textBrowser->jsRef(), localId, url)); // clang-format on
} }
std::string WebRTCClientPage::randomId(size_t length) { std::string WebRTCClientPage::randomId(size_t length) {