mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 12:32:03 -04:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user