Older/WebApplication/WebRTCClientPage.cpp
amass 0af708f4ba
All checks were successful
Deploy / Build (push) Successful in 5m46s
optimize style.
2025-01-12 14:18:06 +08:00

58 lines
2.2 KiB
C++

#include "WebRTCClientPage.h"
#include "BoostLog.h"
#include <Wt/WApplication.h>
#include <Wt/WEnvironment.h>
#include <Wt/WLabel.h>
#include <Wt/WLineEdit.h>
#include <Wt/WPushButton.h>
#include <Wt/WServer.h>
#include <Wt/WTextArea.h>
#include <format>
#include <random>
#include "js/WebRTCClient.js"
WebRTCClientPage::WebRTCClientPage() : Wt::WTemplate(tr("Wt.WebRTC.Home")) {
using namespace Wt;
WApplication *app = WApplication::instance();
app->messageResourceBundle().use(app->appRoot() + "webrtc");
LOAD_JAVASCRIPT(app, "js/WebRTCClient.js", "WebRTCClient", wtjs1);
addStyleClass("bulma-is-flex-grow-1 bulma-is-flex bulma-is-flex-direction-column");
auto localId = randomId(4);
bindNew<Wt::WLabel>("localId")->setText(localId);
auto textBrowser = bindNew<Wt::WTextArea>("textBrowser");
textBrowser->setReadOnly(true);
auto offerId = bindNew<Wt::WLineEdit>("offerId");
offerId->setDisabled(true);
auto offerBtn = bindNew<Wt::WPushButton>("offerBtn");
offerBtn->setText("Offer");
offerBtn->setDisabled(true);
auto sendMsg = bindNew<Wt::WLineEdit>("sendMsg");
sendMsg->setDisabled(true);
auto sendBtn = bindNew<Wt::WPushButton>("sendBtn");
sendBtn->setText("发送");
sendBtn->setDisabled(true);
std::string url = "ws://127.0.0.1:8081/api/v1/webrtc/signal";
setJavaScriptMember(" WebRTCClient", std::format("new {}.WebRTCClient({},{},{},{},{},{},{}, '{}', '{}');", WT_CLASS, WT_CLASS,
jsRef(), offerId->jsRef(), offerBtn->jsRef(), sendMsg->jsRef(),
sendBtn->jsRef(), textBrowser->jsRef(), localId, url));
}
std::string WebRTCClientPage::randomId(size_t length) {
using std::chrono::high_resolution_clock;
static thread_local std::mt19937 rng(static_cast<unsigned int>(high_resolution_clock::now().time_since_epoch().count()));
static const std::string characters("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
std::string id(length, '0');
std::uniform_int_distribution<int> uniform(0, int(characters.size() - 1));
std::generate(id.begin(), id.end(), [&]() { return characters.at(uniform(rng)); });
return id;
}