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; }
|
public ExtendedGuild[] Guilds { get; set; }
|
||||||
[JsonProperty("private_channels")]
|
[JsonProperty("private_channels")]
|
||||||
public Channel[] PrivateChannels { get; set; }
|
public Channel[] PrivateChannels { get; set; }
|
||||||
[JsonProperty("heartbeat_interval")]
|
|
||||||
public int HeartbeatInterval { get; set; }
|
|
||||||
[JsonProperty("relationships")]
|
[JsonProperty("relationships")]
|
||||||
public Relationship[] Relationships { get; set; }
|
public Relationship[] Relationships { get; set; }
|
||||||
|
|
||||||
//Ignored
|
//Ignored
|
||||||
[JsonProperty("user_settings")]
|
/*[JsonProperty("user_settings")]
|
||||||
public object UserSettings { get; set; }
|
|
||||||
[JsonProperty("user_guild_settings")]
|
[JsonProperty("user_guild_settings")]
|
||||||
public object UserGuildSettings { get; set; }
|
[JsonProperty("tutorial")]*/
|
||||||
[JsonProperty("tutorial")]
|
|
||||||
public object Tutorial { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ namespace Discord
|
|||||||
private readonly int _totalShards;
|
private readonly int _totalShards;
|
||||||
private ImmutableDictionary<string, VoiceRegion> _voiceRegions;
|
private ImmutableDictionary<string, VoiceRegion> _voiceRegions;
|
||||||
private string _sessionId;
|
private string _sessionId;
|
||||||
|
private TaskCompletionSource<bool> _connectTask;
|
||||||
|
|
||||||
public int ShardId { get; }
|
public int ShardId { get; }
|
||||||
public ConnectionState ConnectionState { get; private set; }
|
public ConnectionState ConnectionState { get; private set; }
|
||||||
@@ -95,7 +96,7 @@ namespace Discord
|
|||||||
|
|
||||||
_serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
|
_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;
|
ApiClient.ReceivedGatewayEvent += ProcessMessage;
|
||||||
GatewaySocket = config.WebSocketProvider();
|
GatewaySocket = config.WebSocketProvider();
|
||||||
|
|
||||||
@@ -133,8 +134,10 @@ namespace Discord
|
|||||||
ConnectionState = ConnectionState.Connecting;
|
ConnectionState = ConnectionState.Connecting;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
_connectTask = new TaskCompletionSource<bool>();
|
||||||
await ApiClient.Connect().ConfigureAwait(false);
|
await ApiClient.Connect().ConfigureAwait(false);
|
||||||
|
|
||||||
|
await _connectTask.Task.ConfigureAwait(false);
|
||||||
ConnectionState = ConnectionState.Connected;
|
ConnectionState = ConnectionState.Connected;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
@@ -184,9 +187,12 @@ namespace Discord
|
|||||||
internal CachedGuild AddCachedGuild(API.Gateway.ExtendedGuild model, DataStore dataStore = null)
|
internal CachedGuild AddCachedGuild(API.Gateway.ExtendedGuild model, DataStore dataStore = null)
|
||||||
{
|
{
|
||||||
var guild = new CachedGuild(this, model);
|
var guild = new CachedGuild(this, model);
|
||||||
for (int i = 0; i < model.Channels.Length; i++)
|
if (model.Unavailable != true)
|
||||||
AddCachedChannel(model.Channels[i], dataStore);
|
{
|
||||||
DataStore.AddGuild(guild);
|
for (int i = 0; i < model.Channels.Length; i++)
|
||||||
|
AddCachedChannel(model.Channels[i], dataStore);
|
||||||
|
}
|
||||||
|
(dataStore ?? DataStore).AddGuild(guild);
|
||||||
if (model.Large)
|
if (model.Large)
|
||||||
_largeGuilds.Enqueue(model.Id);
|
_largeGuilds.Enqueue(model.Id);
|
||||||
return guild;
|
return guild;
|
||||||
@@ -253,13 +259,20 @@ namespace Discord
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ProcessMessage(GatewayOpCodes opCode, string type, JToken payload)
|
private async Task ProcessMessage(GatewayOpCode opCode, string type, JToken payload)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
switch (opCode)
|
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)
|
switch (type)
|
||||||
{
|
{
|
||||||
//Global
|
//Global
|
||||||
@@ -280,6 +293,8 @@ namespace Discord
|
|||||||
DataStore = dataStore;
|
DataStore = dataStore;
|
||||||
|
|
||||||
await Ready().ConfigureAwait(false);
|
await Ready().ConfigureAwait(false);
|
||||||
|
|
||||||
|
_connectTask.TrySetResult(true); //Signal the .Connect() call to complete
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user