diff --git a/ChatTwo/Http/RouteController.cs b/ChatTwo/Http/RouteController.cs index 69aae86..12f3054 100644 --- a/ChatTwo/Http/RouteController.cs +++ b/ChatTwo/Http/RouteController.cs @@ -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) { diff --git a/ChatTwo/Http/EventServer.cs b/ChatTwo/Http/SSEConnection.cs similarity index 87% rename from ChatTwo/Http/EventServer.cs rename to ChatTwo/Http/SSEConnection.cs index 16f33b5..046d588 100644 --- a/ChatTwo/Http/EventServer.cs +++ b/ChatTwo/Http/SSEConnection.cs @@ -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 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; diff --git a/ChatTwo/Http/ServerCore.cs b/ChatTwo/Http/ServerCore.cs index 5bda16d..f8050e8 100644 --- a/ChatTwo/Http/ServerCore.cs +++ b/ChatTwo/Http/ServerCore.cs @@ -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 EventConnections = []; + internal readonly List EventConnections = []; public ServerCore(Plugin plugin) {