More cleanup for events

This commit is contained in:
Infi
2024-08-24 17:00:54 +02:00
parent c9be7c9c2c
commit c7a52e8edb
3 changed files with 13 additions and 10 deletions
+5 -1
View File
@@ -159,7 +159,7 @@ public class RouteController
{
Plugin.Log.Information($"Client connected: {ctx.Guid}");
var sse = new EventServer(Core.TokenSource.Token);
var sse = new SSEConnection(Core.TokenSource.Token);
Core.EventConnections.Add(sse);
// TODO Check if reconnect or new connection
@@ -167,6 +167,10 @@ public class RouteController
sse.OutboundStack.Push(new NewMessage(messages.ToArray()));
await sse.HandleEventLoop(ctx);
// It should always be done after return
if (sse.Done)
Core.EventConnections.Remove(sse);
}
catch (Exception ex)
{
@@ -5,17 +5,16 @@ using WatsonWebserver.Core;
namespace ChatTwo.Http;
public class EventServer
public class SSEConnection
{
private long Index;
private bool Stopping;
private bool Finished;
private readonly CancellationToken Token;
public bool Done;
public readonly Stack<BaseMessage> OutboundStack = new();
private long Index;
public EventServer(CancellationToken token)
public SSEConnection(CancellationToken token)
{
Token = token;
}
@@ -53,10 +52,10 @@ public class EventServer
}
finally
{
// No Content (204) didn't work for Firefox, so manually closing the connection on client side
// "No Content" (204) didn't work for Firefox, so manually closing the connection on client side
await ctx.Response.SendFinalChunk("data: closing\nevent: close\n\n"u8.ToArray());
Finished = true;
Done = true;
}
}
@@ -67,7 +66,7 @@ public class EventServer
var timeout = 1000; // 1000ms
while (timeout > 0)
{
if (Finished)
if (Done)
break;
timeout -= 100;
+1 -1
View File
@@ -16,7 +16,7 @@ public class ServerCore : IAsyncDisposable
internal readonly CancellationTokenSource TokenSource = new();
internal readonly string StaticDir = Path.Combine(Plugin.Interface.AssemblyLocation.DirectoryName!, "Http");
internal readonly List<EventServer> EventConnections = [];
internal readonly List<SSEConnection> EventConnections = [];
public ServerCore(Plugin plugin)
{