Added connection backoff for OBS.
This commit is contained in:
@ -7,6 +7,7 @@ using System.Collections.Concurrent;
|
|||||||
using TwitchChatTTS.OBS.Socket.Data;
|
using TwitchChatTTS.OBS.Socket.Data;
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using CommonSocketLibrary.Backoff;
|
||||||
|
|
||||||
namespace TwitchChatTTS.OBS.Socket
|
namespace TwitchChatTTS.OBS.Socket
|
||||||
{
|
{
|
||||||
@ -16,8 +17,8 @@ namespace TwitchChatTTS.OBS.Socket
|
|||||||
private readonly IDictionary<string, long> _sourceIds;
|
private readonly IDictionary<string, long> _sourceIds;
|
||||||
private string? URL;
|
private string? URL;
|
||||||
|
|
||||||
|
private readonly IBackoff _backoff;
|
||||||
private readonly Configuration _configuration;
|
private readonly Configuration _configuration;
|
||||||
private System.Timers.Timer _reconnectTimer;
|
|
||||||
|
|
||||||
public bool Connected { get; set; }
|
public bool Connected { get; set; }
|
||||||
public bool Identified { get; set; }
|
public bool Identified { get; set; }
|
||||||
@ -26,6 +27,7 @@ namespace TwitchChatTTS.OBS.Socket
|
|||||||
|
|
||||||
public OBSSocketClient(
|
public OBSSocketClient(
|
||||||
Configuration configuration,
|
Configuration configuration,
|
||||||
|
[FromKeyedServices("hermes")] IBackoff backoff,
|
||||||
[FromKeyedServices("obs")] IEnumerable<IWebSocketHandler> handlers,
|
[FromKeyedServices("obs")] IEnumerable<IWebSocketHandler> handlers,
|
||||||
[FromKeyedServices("obs")] MessageTypeManager<IWebSocketHandler> typeManager,
|
[FromKeyedServices("obs")] MessageTypeManager<IWebSocketHandler> typeManager,
|
||||||
ILogger logger
|
ILogger logger
|
||||||
@ -35,12 +37,9 @@ namespace TwitchChatTTS.OBS.Socket
|
|||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||||
}, logger)
|
}, logger)
|
||||||
{
|
{
|
||||||
|
_backoff = backoff;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
|
|
||||||
_reconnectTimer = new System.Timers.Timer(TimeSpan.FromSeconds(30));
|
|
||||||
_reconnectTimer.Elapsed += async (sender, e) => await Reconnect(e);
|
|
||||||
_reconnectTimer.Enabled = false;
|
|
||||||
|
|
||||||
_requests = new ConcurrentDictionary<string, RequestData>();
|
_requests = new ConcurrentDictionary<string, RequestData>();
|
||||||
_sourceIds = new Dictionary<string, long>();
|
_sourceIds = new Dictionary<string, long>();
|
||||||
}
|
}
|
||||||
@ -51,18 +50,19 @@ namespace TwitchChatTTS.OBS.Socket
|
|||||||
OnConnected += (sender, e) =>
|
OnConnected += (sender, e) =>
|
||||||
{
|
{
|
||||||
Connected = true;
|
Connected = true;
|
||||||
_reconnectTimer.Enabled = false;
|
|
||||||
_logger.Information("OBS websocket client connected.");
|
_logger.Information("OBS websocket client connected.");
|
||||||
};
|
};
|
||||||
|
|
||||||
OnDisconnected += (sender, e) =>
|
OnDisconnected += async (sender, e) =>
|
||||||
{
|
{
|
||||||
_reconnectTimer.Enabled = Identified;
|
|
||||||
_logger.Information($"OBS websocket client disconnected [status: {e.Status}][reason: {e.Reason}] " + (Identified ? "Will be attempting to reconnect every 30 seconds." : "Will not be attempting to reconnect."));
|
_logger.Information($"OBS websocket client disconnected [status: {e.Status}][reason: {e.Reason}] " + (Identified ? "Will be attempting to reconnect every 30 seconds." : "Will not be attempting to reconnect."));
|
||||||
|
|
||||||
Connected = false;
|
Connected = false;
|
||||||
Identified = false;
|
Identified = false;
|
||||||
Streaming = false;
|
Streaming = false;
|
||||||
|
|
||||||
|
if (Identified)
|
||||||
|
await Reconnect(_backoff);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(_configuration.Obs?.Host) && _configuration.Obs?.Port != null)
|
if (!string.IsNullOrWhiteSpace(_configuration.Obs?.Host) && _configuration.Obs?.Port != null)
|
||||||
@ -115,34 +115,6 @@ namespace TwitchChatTTS.OBS.Socket
|
|||||||
await handler.Execute(this, message);
|
await handler.Execute(this, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task Reconnect(ElapsedEventArgs e)
|
|
||||||
{
|
|
||||||
if (Connected)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await DisconnectAsync(new SocketDisconnectionEventArgs(WebSocketCloseStatus.Empty.ToString(), ""));
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
_logger.Error("Failed to disconnect from OBS websocket server.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await Connect();
|
|
||||||
}
|
|
||||||
catch (WebSocketException wse) when (wse.Message.Contains("502"))
|
|
||||||
{
|
|
||||||
_logger.Error($"OBS websocket server cannot be found. Be sure the server is on by looking at OBS > Tools > Websocket Server Settings [code: {wse.ErrorCode}]");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex, "Failed to reconnect to OBS websocket server.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Send(IEnumerable<RequestMessage> messages)
|
public async Task Send(IEnumerable<RequestMessage> messages)
|
||||||
{
|
{
|
||||||
if (!Connected)
|
if (!Connected)
|
||||||
|
@ -138,6 +138,7 @@ s.AddSingleton<IChatterGroupManager, ChatterGroupManager>();
|
|||||||
s.AddSingleton<IGroupPermissionManager, GroupPermissionManager>();
|
s.AddSingleton<IGroupPermissionManager, GroupPermissionManager>();
|
||||||
|
|
||||||
// OBS websocket
|
// OBS websocket
|
||||||
|
s.AddKeyedSingleton<IBackoff>("obs", new ExponentialBackoff(1000, 120 * 1000));
|
||||||
s.AddKeyedSingleton<IWebSocketHandler, HelloHandler>("obs");
|
s.AddKeyedSingleton<IWebSocketHandler, HelloHandler>("obs");
|
||||||
s.AddKeyedSingleton<IWebSocketHandler, IdentifiedHandler>("obs");
|
s.AddKeyedSingleton<IWebSocketHandler, IdentifiedHandler>("obs");
|
||||||
s.AddKeyedSingleton<IWebSocketHandler, RequestResponseHandler>("obs");
|
s.AddKeyedSingleton<IWebSocketHandler, RequestResponseHandler>("obs");
|
||||||
|
Reference in New Issue
Block a user