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}"); 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); Core.EventConnections.Add(sse);
// TODO Check if reconnect or new connection // TODO Check if reconnect or new connection
@@ -167,6 +167,10 @@ public class RouteController
sse.OutboundStack.Push(new NewMessage(messages.ToArray())); sse.OutboundStack.Push(new NewMessage(messages.ToArray()));
await sse.HandleEventLoop(ctx); await sse.HandleEventLoop(ctx);
// It should always be done after return
if (sse.Done)
Core.EventConnections.Remove(sse);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -5,17 +5,16 @@ using WatsonWebserver.Core;
namespace ChatTwo.Http; namespace ChatTwo.Http;
public class EventServer public class SSEConnection
{ {
private long Index;
private bool Stopping; private bool Stopping;
private bool Finished;
private readonly CancellationToken Token; private readonly CancellationToken Token;
public bool Done;
public readonly Stack<BaseMessage> OutboundStack = new(); public readonly Stack<BaseMessage> OutboundStack = new();
private long Index; public SSEConnection(CancellationToken token)
public EventServer(CancellationToken token)
{ {
Token = token; Token = token;
} }
@@ -53,10 +52,10 @@ public class EventServer
} }
finally 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()); 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 var timeout = 1000; // 1000ms
while (timeout > 0) while (timeout > 0)
{ {
if (Finished) if (Done)
break; break;
timeout -= 100; timeout -= 100;
+1 -1
View File
@@ -16,7 +16,7 @@ public class ServerCore : IAsyncDisposable
internal readonly CancellationTokenSource TokenSource = new(); internal readonly CancellationTokenSource TokenSource = new();
internal readonly string StaticDir = Path.Combine(Plugin.Interface.AssemblyLocation.DirectoryName!, "Http"); 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) public ServerCore(Plugin plugin)
{ {