Not waiting for the response, command list endpoint
Returning the response right after the command is executed, most of the time that's enough, long polling should be used in addition
This commit is contained in:
parent
f909fce1b7
commit
7a6cc79e61
3 changed files with 37 additions and 12 deletions
|
@ -19,15 +19,15 @@ namespace TBConsole
|
||||||
public override void OnApplicationStart()
|
public override void OnApplicationStart()
|
||||||
{
|
{
|
||||||
TechbloxModdingAPI.Main.Init();
|
TechbloxModdingAPI.Main.Init();
|
||||||
_server = new WebServer(CommandReceived);
|
_server = new WebServer(CommandReceived, GetCommandList);
|
||||||
_server.Start();
|
_server.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<string> CommandReceived(string command)
|
private string CommandReceived(string command)
|
||||||
{
|
{
|
||||||
if (_logHandler == null)
|
if (_logHandler == null)
|
||||||
Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler);
|
Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler);
|
||||||
var logTask = _logHandler.CollectLogMessages();
|
_logHandler.StartCollectingLogMessages();
|
||||||
bool inString = false;
|
bool inString = false;
|
||||||
var cmdparts = new List<string>();
|
var cmdparts = new List<string>();
|
||||||
command = command.Trim();
|
command = command.Trim();
|
||||||
|
@ -62,10 +62,16 @@ namespace TBConsole
|
||||||
return "Too many arguments! Maximum for default commands is 3";
|
return "Too many arguments! Maximum for default commands is 3";
|
||||||
}
|
}
|
||||||
|
|
||||||
string result = await logTask;
|
string result = _logHandler.FinishCollectingLogMessages();
|
||||||
return $"Got it: {command}\n{result}";
|
return $"Got it: {command}\n{result}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetCommandList()
|
||||||
|
{
|
||||||
|
return ExistingCommands.GetCommandNamesAndDescriptions()
|
||||||
|
.Select(command => command.Name + " - " + command.Description).Aggregate((a, b) => a + "\n" + b);
|
||||||
|
}
|
||||||
|
|
||||||
public override void OnApplicationQuit()
|
public override void OnApplicationQuit()
|
||||||
{
|
{
|
||||||
_server.Stop();
|
_server.Stop();
|
||||||
|
@ -75,7 +81,7 @@ namespace TBConsole
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var mod = new TBConsoleMod();
|
var mod = new TBConsoleMod();
|
||||||
mod._server = new WebServer(mod.CommandReceived);
|
mod._server = new WebServer(mod.CommandReceived, mod.GetCommandList);
|
||||||
mod._server.Start();
|
mod._server.Start();
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,10 +23,10 @@ namespace TBConsole
|
||||||
_original.LogException(exception, context);
|
_original.LogException(exception, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> CollectLogMessages()
|
public void StartCollectingLogMessages() => _collectedLog = "";
|
||||||
|
|
||||||
|
public string FinishCollectingLogMessages()
|
||||||
{
|
{
|
||||||
_collectedLog = "";
|
|
||||||
await Task.Delay(1000);
|
|
||||||
string ret = _collectedLog;
|
string ret = _collectedLog;
|
||||||
_collectedLog = null;
|
_collectedLog = null;
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -9,8 +9,14 @@ namespace TBConsole
|
||||||
{
|
{
|
||||||
private bool _running;
|
private bool _running;
|
||||||
private readonly HttpListener _listener = new HttpListener();
|
private readonly HttpListener _listener = new HttpListener();
|
||||||
private Func<string, Task<string>> _receiver;
|
private readonly Func<string, string> _commandReceiver;
|
||||||
public WebServer(Func<string, Task<string>> receiver) => _receiver = receiver;
|
private readonly Func<string> _commandsListSender;
|
||||||
|
|
||||||
|
public WebServer(Func<string, string> commandReceiver, Func<string> commandsListSender)
|
||||||
|
{
|
||||||
|
_commandReceiver = commandReceiver;
|
||||||
|
_commandsListSender = commandsListSender;
|
||||||
|
}
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
|
@ -33,7 +39,20 @@ namespace TBConsole
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var context = await _listener.GetContextAsync();
|
var context = await _listener.GetContextAsync();
|
||||||
string resp = await _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync());
|
string request = await new StreamReader(context.Request.InputStream).ReadToEndAsync();
|
||||||
|
string resp;
|
||||||
|
switch (context.Request.Url.AbsolutePath)
|
||||||
|
{
|
||||||
|
case "/command":
|
||||||
|
resp = _commandReceiver(request);
|
||||||
|
break;
|
||||||
|
case "/commands":
|
||||||
|
resp = _commandsListSender();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
resp = "<img src=\"https://http.cat/404\">";
|
||||||
|
break;
|
||||||
|
}
|
||||||
string origin = context.Request.Headers["Origin"];
|
string origin = context.Request.Headers["Origin"];
|
||||||
if (origin == "http://localhost:4200" || origin == "https://tbconsole.web.app")
|
if (origin == "http://localhost:4200" || origin == "https://tbconsole.web.app")
|
||||||
context.Response.AddHeader("Access-Control-Allow-Origin", origin);
|
context.Response.AddHeader("Access-Control-Allow-Origin", origin);
|
||||||
|
|
Loading…
Reference in a new issue