Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ namespace facebook::react {
DevServerHelper::DevServerHelper(
std::string appId,
std::string deviceName,
std::string devServerHost,
uint32_t devServerPort,
const HttpClientFactory& httpClientFactory,
JavaScriptModuleCallback javaScriptModuleCallback) noexcept
: appId_(std::move(appId)),
deviceName_(std::move(deviceName)),
devServerHost_(std::move(devServerHost)),
devServerPort_(devServerPort),
httpClient_(httpClientFactory()),
javaScriptModuleCallback_(std::move(javaScriptModuleCallback)) {
deviceId_ = SHA256(fmt::format("{}-{}", deviceName_, appId_));
Expand Down Expand Up @@ -101,8 +105,8 @@ std::string DevServerHelper::getInspectorUrl() const {

return fmt::format(
"ws://{}:{}/inspector/device?name={}&app={}&device={}&profiling={}",
DEFAULT_DEV_SERVER_HOST,
DEFAULT_DEV_SERVER_PORT,
devServerHost_,
devServerPort_,
urlEscape(deviceName_),
appId_,
deviceId_,
Expand All @@ -122,8 +126,8 @@ std::string DevServerHelper::getBundleUrl() const {
bool runModule = !splitBundle;
return fmt::format(
"http://{}:{}/{}.bundle?platform={}&dev={}&lazy={}&minify={}&app={}&modulesOnly={}&runModule={}&inlineSourceMap=false&excludeSource=true&sourcePaths=url-server",
DEFAULT_DEV_SERVER_HOST,
DEFAULT_DEV_SERVER_PORT,
devServerHost_,
devServerPort_,
sourcePath_,
DEFAULT_PLATFORM,
dev,
Expand All @@ -135,15 +139,14 @@ std::string DevServerHelper::getBundleUrl() const {
};

std::string DevServerHelper::getPackagerConnectionUrl() const {
return fmt::format(
"ws://{}:{}/message", DEFAULT_DEV_SERVER_HOST, DEFAULT_DEV_SERVER_PORT);
return fmt::format("ws://{}:{}/message", devServerHost_, devServerPort_);
}

void DevServerHelper::openDebugger() const {
auto requestUrl = fmt::format(
"http://{}:{}/open-debugger?device={}",
DEFAULT_DEV_SERVER_HOST,
DEFAULT_DEV_SERVER_PORT,
devServerHost_,
devServerPort_,
deviceId_);
httpClient_->sendRequest({}, "POST", requestUrl);
}
Expand All @@ -152,8 +155,8 @@ void DevServerHelper::setupHMRClient() const {
folly::dynamic params = folly::dynamic::array(
DEFAULT_PLATFORM,
sourcePath_,
DEFAULT_DEV_SERVER_HOST,
DEFAULT_DEV_SERVER_PORT,
devServerHost_,
devServerPort_,
true /*enable*/);
javaScriptModuleCallback_("HMRClient", "setup", std::move(params));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

namespace facebook::react {

namespace {

constexpr std::string_view DEFAULT_DEV_SERVER_HOST = "localhost";
constexpr uint32_t DEFAULT_DEV_SERVER_PORT = 8081;

} // namespace

class DevServerHelper {
public:
enum class DownloadProgressStatus : short { STARTED, FAILED, FINISHED };
Expand All @@ -31,6 +24,8 @@ class DevServerHelper {
DevServerHelper(
std::string appId,
std::string deviceName,
std::string devServerHost,
uint32_t devServerPort,
const HttpClientFactory& httpClientFactory,
JavaScriptModuleCallback javaScriptModuleCallback) noexcept;
~DevServerHelper() noexcept = default;
Expand All @@ -56,6 +51,8 @@ class DevServerHelper {
private:
std::string appId_;
std::string deviceName_;
std::string devServerHost_;
uint32_t devServerPort_;
std::unique_ptr<IHttpClient> httpClient_;
JavaScriptModuleCallback javaScriptModuleCallback_;
std::string deviceId_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,49 @@ InspectorPackagerConnectionDelegate::WebSocket::WebSocket(
inspectorThread_(std::move(inspectorThread)) {
websocket_ = webSocketClientFactory();
websocket_->setOnMessageCallback(
[webSocketDelegate](const std::string& message) {
if (const auto strongDelegate = webSocketDelegate.lock()) {
strongDelegate->didReceiveMessage(message);
[webSocketDelegate,
inspectorThread = inspectorThread_](const std::string& message) {
auto strongInspectorThread = inspectorThread.lock();
if (!strongInspectorThread) {
return;
}
strongInspectorThread->invokeElsePost([webSocketDelegate, message]() {
if (const auto strongDelegate = webSocketDelegate.lock()) {
strongDelegate->didReceiveMessage(message);
}
});
});
websocket_->setOnClosedCallback(
[webSocketDelegate](const std::string& /*message*/) {
if (const auto strongDelegate = webSocketDelegate.lock()) {
strongDelegate->didClose();
[webSocketDelegate,
inspectorThread = inspectorThread_](const std::string& /*message*/) {
auto strongInspectorThread = inspectorThread.lock();
if (!strongInspectorThread) {
return;
}
strongInspectorThread->invokeElsePost([webSocketDelegate]() {
if (const auto strongDelegate = webSocketDelegate.lock()) {
strongDelegate->didClose();
}
});
});
websocket_->connect(
url, [webSocketDelegate](bool success, const std::string& message) {
const auto strongDelegate = webSocketDelegate.lock();
if (!strongDelegate) {
url,
[webSocketDelegate, inspectorThread = inspectorThread_](
bool success, const std::string& message) {
auto strongInspectorThread = inspectorThread.lock();
if (!strongInspectorThread) {
return;
}

if (success) {
strongDelegate->didOpen();
} else {
strongDelegate->didFailWithError(std::nullopt, message);
}
strongInspectorThread->invokeElsePost(
[webSocketDelegate, success, message]() {
if (auto strongDelegate = webSocketDelegate.lock()) {
if (success) {
strongDelegate->didOpen();
} else {
strongDelegate->didFailWithError(std::nullopt, message);
}
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ void ReactHost::createReactInstance() {
devServerHelper_ = std::make_shared<DevServerHelper>(
reactInstanceConfig_.appId,
reactInstanceConfig_.deviceName,
reactInstanceConfig_.devServerHost,
reactInstanceConfig_.devServerPort,
httpClientFactory,
[this](
const std::string& moduleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct ReactInstanceConfig {
#endif
std::string appId;
std::string deviceName;
std::string devServerHost{"localhost"};
uint32_t devServerPort{8081};
};

} // namespace facebook::react
Loading