feat: begin working on client

This commit is contained in:
Stevan Freeborn
2025-05-13 09:54:38 -05:00
parent 48f12795a5
commit a25e3194db
4 changed files with 59 additions and 1 deletions
@@ -0,0 +1,48 @@
namespace StevesBot.Worker.Discord;
internal class DiscordGatewayClient : IDiscordGatewayClient
{
private readonly DiscordGatewayClientOptions _options;
private readonly IWebSocketFactory _webSocketFactory;
private readonly ILogger<DiscordGatewayClient> _logger;
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
Converters =
{
new DiscordEventConverter(),
},
};
private readonly AsyncLock _lock = new();
private string _gatewayUrl = string.Empty;
private IWebSocket? _webSocket;
private Task? _receiveTask;
private DateTime _timeLastHeartbeatSent = DateTime.MinValue;
private DateTime _timeLastHeartbeatAcknowledged = DateTime.MinValue;
private CancellationTokenSource? _heartbeatCts;
private Task? _heartbeatTask;
private string _sessionId = string.Empty;
private string _resumeGatewayUrl = string.Empty;
public DiscordGatewayClient(
DiscordGatewayClientOptions options,
IWebSocketFactory webSocketFactory,
ILogger<DiscordGatewayClient> logger
)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
_webSocketFactory = webSocketFactory ?? throw new ArgumentNullException(nameof(webSocketFactory));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public void Dispose()
{
_heartbeatCts?.Cancel();
_heartbeatCts?.Dispose();
_heartbeatTask?.Dispose();
_receiveTask?.Dispose();
_webSocket?.Dispose();
}
}
@@ -0,0 +1,5 @@
namespace StevesBot.Worker.Discord;
internal interface IDiscordGatewayClient : IDisposable
{
}
+5
View File
@@ -1,3 +1,8 @@
global using System.Net.WebSockets; global using System.Net.WebSockets;
global using System.Text.Json; global using System.Text.Json;
global using System.Text.Json.Serialization; global using System.Text.Json.Serialization;
global using StevesBot.Worker.Discord.Events;
global using StevesBot.Worker.Threading;
global using StevesBot.Worker.WebSockets;
@@ -5,7 +5,7 @@
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"DiscordClientOptions": { "DiscordGatewayClientOptions": {
"ApiUrl": "ApiUrl", "ApiUrl": "ApiUrl",
"AppToken": "AppToken", "AppToken": "AppToken",
"Intents": 0 "Intents": 0