Ver código fonte

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
tags/v1.0.0
NorbiPeti 3 anos atrás
pai
commit
7a6cc79e61
Acessado por: NorbiPeti <szatmari.norbert.peter@gmail.com> ID da chave GPG: DBA4C4549A927E56
3 arquivos alterados com 37 adições e 12 exclusões
  1. +11
    -5
      TBConsole/TBConsoleMod.cs
  2. +3
    -3
      TBConsole/UnityLogHandler.cs
  3. +23
    -4
      TBConsole/WebServer.cs

+ 11
- 5
TBConsole/TBConsoleMod.cs Ver arquivo

@@ -19,15 +19,15 @@ namespace TBConsole
public override void OnApplicationStart()
{
TechbloxModdingAPI.Main.Init();
_server = new WebServer(CommandReceived);
_server = new WebServer(CommandReceived, GetCommandList);
_server.Start();
}

private async Task<string> CommandReceived(string command)
private string CommandReceived(string command)
{
if (_logHandler == null)
Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler);
var logTask = _logHandler.CollectLogMessages();
_logHandler.StartCollectingLogMessages();
bool inString = false;
var cmdparts = new List<string>();
command = command.Trim();
@@ -62,10 +62,16 @@ namespace TBConsole
return "Too many arguments! Maximum for default commands is 3";
}

string result = await logTask;
string result = _logHandler.FinishCollectingLogMessages();
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()
{
_server.Stop();
@@ -75,7 +81,7 @@ namespace TBConsole
public static void Main(string[] args)
{
var mod = new TBConsoleMod();
mod._server = new WebServer(mod.CommandReceived);
mod._server = new WebServer(mod.CommandReceived, mod.GetCommandList);
mod._server.Start();
Console.ReadLine();
}


+ 3
- 3
TBConsole/UnityLogHandler.cs Ver arquivo

@@ -23,10 +23,10 @@ namespace TBConsole
_original.LogException(exception, context);
}

public async Task<string> CollectLogMessages()
public void StartCollectingLogMessages() => _collectedLog = "";

public string FinishCollectingLogMessages()
{
_collectedLog = "";
await Task.Delay(1000);
string ret = _collectedLog;
_collectedLog = null;
return ret;


+ 23
- 4
TBConsole/WebServer.cs Ver arquivo

@@ -9,9 +9,15 @@ namespace TBConsole
{
private bool _running;
private readonly HttpListener _listener = new HttpListener();
private Func<string, Task<string>> _receiver;
public WebServer(Func<string, Task<string>> receiver) => _receiver = receiver;
private readonly Func<string, string> _commandReceiver;
private readonly Func<string> _commandsListSender;

public WebServer(Func<string, string> commandReceiver, Func<string> commandsListSender)
{
_commandReceiver = commandReceiver;
_commandsListSender = commandsListSender;
}

public void Start()
{
_running = true;
@@ -33,7 +39,20 @@ namespace TBConsole
try
{
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"];
if (origin == "http://localhost:4200" || origin == "https://tbconsole.web.app")
context.Response.AddHeader("Access-Control-Allow-Origin", origin);


Carregando…
Cancelar
Salvar