The Final Solution to the Websocket question? I've gone through like 10 different iterations to try and get ByServer reconnections to work correctly. Now just disabled reconnection altogether and I'm manually disposing and recreating the instance whenever it dies using a watchdog task. So far working great after 12 hours!

This commit is contained in:
barelyprofessional
2024-07-20 10:42:38 +10:00
parent 2574d278a7
commit 8676241fbf
6 changed files with 158 additions and 100 deletions

View File

@@ -7,7 +7,7 @@ using Websocket.Client;
namespace KfChatDotNetKickBot.Services;
public class TwitchChat
public class TwitchChat : IDisposable
{
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private WebsocketClient _wsClient;
@@ -54,7 +54,8 @@ public class TwitchChat
var client = new WebsocketClient(_wsUri, factory)
{
ReconnectTimeout = TimeSpan.FromSeconds(ReconnectTimeout)
ReconnectTimeout = TimeSpan.FromSeconds(ReconnectTimeout),
IsReconnectionEnabled = false
};
client.ReconnectionHappened.Subscribe(WsReconnection);
@@ -79,11 +80,6 @@ public class TwitchChat
_logger.Error($"Close Status => {disconnectionInfo.CloseStatus}; Close Status Description => {disconnectionInfo.CloseStatusDescription}");
_logger.Error(disconnectionInfo.Exception);
OnWsDisconnection?.Invoke(this, disconnectionInfo);
if (disconnectionInfo.Type == DisconnectionType.ByServer)
{
_logger.Info("Forcing reconnection as the type was ByServer");
_wsClient.Reconnect().Wait(_cancellationToken);
}
}
private void WsReconnection(ReconnectionInfo reconnectionInfo)
@@ -150,7 +146,7 @@ public class TwitchChat
switch (command)
{
case "PING":
_logger.Debug("Received PING, sending PONG");
_logger.Info("Received PING, sending PONG");
_wsClient.Send("PONG");
return;
case "JOIN":
@@ -175,4 +171,10 @@ public class TwitchChat
_logger.Error("--- End of IRC Message ---");
}
}
public void Dispose()
{
_wsClient.Dispose();
GC.SuppressFinalize(this);
}
}