#include "WebRTCClientPage.h" #include "BoostLog.h" #include #include #include #include #include #include #include #include #include #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("localId")->setText(localId); auto textBrowser = bindNew("textBrowser"); textBrowser->setReadOnly(true); auto offerId = bindNew("offerId"); offerId->setDisabled(true); auto offerBtn = bindNew("offerBtn"); offerBtn->setText("Offer"); offerBtn->setDisabled(true); auto sendMsg = bindNew("sendMsg"); sendMsg->setDisabled(true); auto sendBtn = bindNew("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(high_resolution_clock::now().time_since_epoch().count())); static const std::string characters("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); std::string id(length, '0'); std::uniform_int_distribution uniform(0, int(characters.size() - 1)); std::generate(id.begin(), id.end(), [&]() { return characters.at(uniform(rng)); }); return id; }