Gateway disposes the subscriptions when closed

This commit is contained in:
Kecskeméti László 2024-07-24 11:07:04 +02:00
parent 9c8bedf581
commit b01ba1e2cd

View File

@ -7,13 +7,14 @@ namespace Discord.API;
public abstract class AbstractGateway { public abstract class AbstractGateway {
protected readonly IWebsocketClient WebsocketClient; protected readonly IWebsocketClient WebsocketClient;
protected readonly TimeProvider TimeProvider; protected readonly TimeProvider TimeProvider;
private List<IDisposable> Subscriptions = new(3);
public AbstractGateway(IWebsocketClient client, TimeProvider time_provider){ public AbstractGateway(IWebsocketClient client, TimeProvider time_provider){
this.WebsocketClient = client; this.WebsocketClient = client;
this.TimeProvider = time_provider; this.TimeProvider = time_provider;
WebsocketClient.DisconnectionHappened.Subscribe(DisconnectHandlerInternal); Subscriptions.Add(WebsocketClient.DisconnectionHappened.Subscribe(DisconnectHandlerInternal));
WebsocketClient.ReconnectionHappened.Subscribe(ReconnectHandler); Subscriptions.Add(WebsocketClient.ReconnectionHappened.Subscribe(ReconnectHandler));
WebsocketClient.MessageReceived.Subscribe(MessageReceivedHandler); Subscriptions.Add(WebsocketClient.MessageReceived.Subscribe(MessageReceivedHandler));
WebsocketClient.Start(); WebsocketClient.Start();
} }
@ -100,5 +101,9 @@ public abstract class AbstractGateway {
public async Task Close(){ public async Task Close(){
StopHeartbeat(); StopHeartbeat();
await WebsocketClient.Stop(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "Shutdown"); await WebsocketClient.Stop(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "Shutdown");
foreach (var sub in Subscriptions)
{
sub.Dispose();
}
} }
} }