Delayed .Connect completion, added Hello support
This commit is contained in:
10
src/Discord.Net/API/Gateway/HelloEvent.cs
Normal file
10
src/Discord.Net/API/Gateway/HelloEvent.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Discord.API.Gateway
|
||||
{
|
||||
public class HelloEvent
|
||||
{
|
||||
[JsonProperty("heartbeat_interval")]
|
||||
public int HeartbeatInterval { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -26,17 +26,12 @@ namespace Discord.API.Gateway
|
||||
public ExtendedGuild[] Guilds { get; set; }
|
||||
[JsonProperty("private_channels")]
|
||||
public Channel[] PrivateChannels { get; set; }
|
||||
[JsonProperty("heartbeat_interval")]
|
||||
public int HeartbeatInterval { get; set; }
|
||||
[JsonProperty("relationships")]
|
||||
public Relationship[] Relationships { get; set; }
|
||||
|
||||
//Ignored
|
||||
[JsonProperty("user_settings")]
|
||||
public object UserSettings { get; set; }
|
||||
/*[JsonProperty("user_settings")]
|
||||
[JsonProperty("user_guild_settings")]
|
||||
public object UserGuildSettings { get; set; }
|
||||
[JsonProperty("tutorial")]
|
||||
public object Tutorial { get; set; }
|
||||
[JsonProperty("tutorial")]*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ namespace Discord
|
||||
private readonly int _totalShards;
|
||||
private ImmutableDictionary<string, VoiceRegion> _voiceRegions;
|
||||
private string _sessionId;
|
||||
private TaskCompletionSource<bool> _connectTask;
|
||||
|
||||
public int ShardId { get; }
|
||||
public ConnectionState ConnectionState { get; private set; }
|
||||
@@ -95,7 +96,7 @@ namespace Discord
|
||||
|
||||
_serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
|
||||
|
||||
ApiClient.SentGatewayMessage += async opCode => await _gatewayLogger.Verbose($"Sent Op {opCode}");
|
||||
ApiClient.SentGatewayMessage += async opCode => await _gatewayLogger.Verbose($"Sent Op {(GatewayOpCode)opCode}");
|
||||
ApiClient.ReceivedGatewayEvent += ProcessMessage;
|
||||
GatewaySocket = config.WebSocketProvider();
|
||||
|
||||
@@ -133,8 +134,10 @@ namespace Discord
|
||||
ConnectionState = ConnectionState.Connecting;
|
||||
try
|
||||
{
|
||||
_connectTask = new TaskCompletionSource<bool>();
|
||||
await ApiClient.Connect().ConfigureAwait(false);
|
||||
|
||||
await _connectTask.Task.ConfigureAwait(false);
|
||||
ConnectionState = ConnectionState.Connected;
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -184,9 +187,12 @@ namespace Discord
|
||||
internal CachedGuild AddCachedGuild(API.Gateway.ExtendedGuild model, DataStore dataStore = null)
|
||||
{
|
||||
var guild = new CachedGuild(this, model);
|
||||
for (int i = 0; i < model.Channels.Length; i++)
|
||||
AddCachedChannel(model.Channels[i], dataStore);
|
||||
DataStore.AddGuild(guild);
|
||||
if (model.Unavailable != true)
|
||||
{
|
||||
for (int i = 0; i < model.Channels.Length; i++)
|
||||
AddCachedChannel(model.Channels[i], dataStore);
|
||||
}
|
||||
(dataStore ?? DataStore).AddGuild(guild);
|
||||
if (model.Large)
|
||||
_largeGuilds.Enqueue(model.Id);
|
||||
return guild;
|
||||
@@ -253,13 +259,20 @@ namespace Discord
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task ProcessMessage(GatewayOpCodes opCode, string type, JToken payload)
|
||||
private async Task ProcessMessage(GatewayOpCode opCode, string type, JToken payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (opCode)
|
||||
{
|
||||
case GatewayOpCodes.Dispatch:
|
||||
case GatewayOpCode.Hello:
|
||||
{
|
||||
var data = payload.ToObject<HelloEvent>(_serializer);
|
||||
|
||||
await ApiClient.SendIdentify().ConfigureAwait(false);
|
||||
}
|
||||
break;
|
||||
case GatewayOpCode.Dispatch:
|
||||
switch (type)
|
||||
{
|
||||
//Global
|
||||
@@ -280,6 +293,8 @@ namespace Discord
|
||||
DataStore = dataStore;
|
||||
|
||||
await Ready().ConfigureAwait(false);
|
||||
|
||||
_connectTask.TrySetResult(true); //Signal the .Connect() call to complete
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user