Skip to content

Commit 253e4fa

Browse files
author
Nako Sung
committed
Add http server to serve node.js style debugger
1 parent b8325f0 commit 253e4fa

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

Source/V8/Private/Inspector.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,20 @@ class FInspector : public IJavascriptInspector, public FTickableAnyObject, publi
250250
bool IsAlive{ false };
251251
lws_context* WebSocketContext;
252252
lws_protocols* WebSocketProtocols;
253+
int32 Port;
254+
255+
FString WebSocketDebuggerUrl() const
256+
{
257+
return FString::Printf(TEXT("ws://127.0.0.1:%d"), Port);
258+
}
259+
260+
FString DevToolsFrontEndUrl() const
261+
{
262+
return FString::Printf(TEXT("chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:%d"), Port);
263+
}
253264

254265
FInspector(v8::Platform* platform, int32 InPort, Local<Context> InContext)
266+
: Port(InPort)
255267
{
256268
platform_ = platform;
257269
isolate_ = InContext->GetIsolate();
@@ -282,7 +294,7 @@ class FInspector : public IJavascriptInspector, public FTickableAnyObject, publi
282294
auto result = script->Run();
283295
}
284296

285-
UE_LOG(Javascript, Log, TEXT("open chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=localhost:%d"), InPort);
297+
UE_LOG(Javascript, Log, TEXT("open %s"), *DevToolsFrontEndUrl());
286298

287299
InstallRelay();
288300
}
@@ -438,7 +450,52 @@ class FInspector : public IJavascriptInspector, public FTickableAnyObject, publi
438450
lws_set_timeout(Wsi, NO_PENDING_TIMEOUT, 0);
439451
}
440452
break;
453+
case LWS_CALLBACK_HTTP:
454+
{
455+
auto request = (char*)In;
456+
457+
UE_LOG(Javascript, Log, TEXT("requested URI:%s"), UTF8_TO_TCHAR(request));
458+
459+
FString res[][2] = {
460+
{
461+
TEXT("description"), TEXT("unreal.js instance")
462+
},
463+
{
464+
TEXT("devtoolsFrontendUrl"), DevToolsFrontEndUrl()
465+
},
466+
{
467+
TEXT("type"), TEXT("node")
468+
},
469+
{
470+
TEXT("id"), TEXT("0")
471+
},
472+
{
473+
TEXT("title"), TEXT("unreal.js")
474+
},
475+
{
476+
TEXT("webSocketDebuggerUrl"), WebSocketDebuggerUrl()
477+
}
478+
};
441479

480+
TArray<FString> res2;
481+
for (auto pair : res)
482+
{
483+
res2.Add(FString::Printf(TEXT("\"%s\" : \"%s\""), *pair[0], *pair[1]));
484+
}
485+
486+
{
487+
auto response = FString(TEXT("HTTP/1.0 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nCache-Control: no-cache\r\n\r\n"));
488+
FTCHARToUTF8 utf8(*response);
489+
lws_write_http(Wsi, utf8.Get(), utf8.Length());
490+
}
491+
492+
{
493+
auto response = FString::Printf(TEXT("[{%s}]"), *FString::Join(res2, TEXT(",")));
494+
FTCHARToUTF8 utf8(*response);
495+
lws_write_http(Wsi, utf8.Get(), utf8.Length());
496+
}
497+
}
498+
break;
442499
case LWS_CALLBACK_RECEIVE:
443500
{
444501
auto Remaining = lws_remaining_packet_payload(Wsi);

0 commit comments

Comments
 (0)