Added events for forwarding packets
This commit is contained in:
parent
b01ba1e2cd
commit
ad7597379a
@ -1,4 +1,5 @@
|
|||||||
using Discord.API.Rest;
|
using System.Reactive.Subjects;
|
||||||
|
using Discord.API.Rest;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace Discord.API;
|
namespace Discord.API;
|
||||||
@ -13,6 +14,10 @@ public class DiscordClient
|
|||||||
private GatewayClient? Gateway;
|
private GatewayClient? Gateway;
|
||||||
private TimeProvider TimeProvider;
|
private TimeProvider TimeProvider;
|
||||||
|
|
||||||
|
private readonly Subject<GatewayPacket> PacketStream = new Subject<GatewayPacket>();
|
||||||
|
public IObservable<GatewayPacket> PacketReceived => PacketStream;
|
||||||
|
private IDisposable? Subscription;
|
||||||
|
|
||||||
public DiscordClient(string api_key, Intents intents){
|
public DiscordClient(string api_key, Intents intents){
|
||||||
TimeProvider = TimeProvider.System;
|
TimeProvider = TimeProvider.System;
|
||||||
this.Intents=intents;
|
this.Intents=intents;
|
||||||
@ -46,6 +51,7 @@ public class DiscordClient
|
|||||||
Log.Information("DiscordClient: Gateway url: {gateway_url}", resp.Value.Url);
|
Log.Information("DiscordClient: Gateway url: {gateway_url}", resp.Value.Url);
|
||||||
|
|
||||||
Gateway = new GatewayClient(resp.Value.Url, ApiKey, TimeProvider, (ulong)Intents);
|
Gateway = new GatewayClient(resp.Value.Url, ApiKey, TimeProvider, (ulong)Intents);
|
||||||
|
Subscription = Gateway.PacketReceived.Subscribe(PacketStream);
|
||||||
|
|
||||||
Log.Information("DiscordClient: Started gateway");
|
Log.Information("DiscordClient: Started gateway");
|
||||||
}
|
}
|
||||||
@ -53,5 +59,6 @@ public class DiscordClient
|
|||||||
public async Task Close(){
|
public async Task Close(){
|
||||||
if(Gateway is not null)
|
if(Gateway is not null)
|
||||||
await Gateway.Close();
|
await Gateway.Close();
|
||||||
|
Subscription?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -98,7 +98,7 @@ public abstract class AbstractGateway {
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public async Task Close(){
|
public virtual 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)
|
foreach (var sub in Subscriptions)
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
|
using System.Reactive.Subjects;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Websocket.Client;
|
using Websocket.Client;
|
||||||
@ -15,6 +16,9 @@ public class GatewayClient : AbstractGateway {
|
|||||||
private string? SessionId = null;
|
private string? SessionId = null;
|
||||||
private Uri StartingUri;
|
private Uri StartingUri;
|
||||||
|
|
||||||
|
private Subject<GatewayPacket> PacketStream = new();
|
||||||
|
public IObservable<GatewayPacket> PacketReceived => PacketStream;
|
||||||
|
|
||||||
public GatewayClient(string url, string api_key, TimeProvider time_provider, ulong intents)
|
public GatewayClient(string url, string api_key, TimeProvider time_provider, ulong intents)
|
||||||
: this(url, api_key, time_provider, uri => new WebsocketClient(uri), intents)
|
: this(url, api_key, time_provider, uri => new WebsocketClient(uri), intents)
|
||||||
{ }
|
{ }
|
||||||
@ -43,6 +47,7 @@ public class GatewayClient : AbstractGateway {
|
|||||||
if((int?)info.CloseStatus is 4004 or 4008 or (>= 4010 and <= 4014)){
|
if((int?)info.CloseStatus is 4004 or 4008 or (>= 4010 and <= 4014)){
|
||||||
Log.Error("GATEWAY: Disconnection due to error: close code: {code}, desc: {desc}", info.CloseStatus, info.CloseStatusDescription);
|
Log.Error("GATEWAY: Disconnection due to error: close code: {code}, desc: {desc}", info.CloseStatus, info.CloseStatusDescription);
|
||||||
info.CancelReconnection = true;
|
info.CancelReconnection = true;
|
||||||
|
PacketStream.OnError(new Exception("Gateway failed to authenticate"));
|
||||||
}else if((int?)info.CloseStatus is 4007 or 4009){
|
}else if((int?)info.CloseStatus is 4007 or 4009){
|
||||||
SessionId = null;
|
SessionId = null;
|
||||||
Sequence = null;
|
Sequence = null;
|
||||||
@ -77,6 +82,8 @@ public class GatewayClient : AbstractGateway {
|
|||||||
Log.Debug("GATEWAY: Packet not handled {opcode}", packet.Op);
|
Log.Debug("GATEWAY: Packet not handled {opcode}", packet.Op);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PacketStream.OnNext(packet);
|
||||||
}catch(Exception ex){
|
}catch(Exception ex){
|
||||||
Log.Warning(ex, "GATEWAY: Error processing gateway event");
|
Log.Warning(ex, "GATEWAY: Error processing gateway event");
|
||||||
}
|
}
|
||||||
@ -160,4 +167,10 @@ public class GatewayClient : AbstractGateway {
|
|||||||
Log.Debug("GATEWAY: Identifying for a new session");
|
Log.Debug("GATEWAY: Identifying for a new session");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override async Task Close()
|
||||||
|
{
|
||||||
|
PacketStream.OnCompleted();
|
||||||
|
await base.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,9 @@ Log.Logger=new LoggerConfiguration()
|
|||||||
string api_key = File.ReadAllText("api_key.txt");
|
string api_key = File.ReadAllText("api_key.txt");
|
||||||
|
|
||||||
DiscordClient client = new DiscordClient(api_key, Intents.Guilds);
|
DiscordClient client = new DiscordClient(api_key, Intents.Guilds);
|
||||||
|
client.PacketReceived.Subscribe(packet=>{
|
||||||
|
Console.WriteLine($"Packet {packet.Op} was received");
|
||||||
|
});
|
||||||
|
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user