diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs
index 9fc717762..dcee36736 100644
--- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs
+++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs
@@ -139,9 +139,9 @@ namespace Discord.WebSocket
internal override async Task OnLoginAsync(TokenType tokenType, string token)
{
+ var botGateway = await GetBotGatewayAsync().ConfigureAwait(false);
if (_automaticShards)
{
- var botGateway = await GetBotGatewayAsync().ConfigureAwait(false);
_shardIds = Enumerable.Range(0, botGateway.Shards).ToArray();
_totalShards = _shardIds.Length;
_shards = new DiscordSocketClient[_shardIds.Length];
@@ -163,7 +163,12 @@ namespace Discord.WebSocket
//Assume thread safe: already in a connection lock
for (int i = 0; i < _shards.Length; i++)
+ {
+ // Set the gateway URL to the one returned by Discord, if a custom one isn't set.
+ _shards[i].ApiClient.GatewayUrl = botGateway.Url;
+
await _shards[i].LoginAsync(tokenType, token);
+ }
if(_defaultStickers.Length == 0 && _baseConfig.AlwaysDownloadDefaultStickers)
await DownloadDefaultStickersAsync().ConfigureAwait(false);
@@ -175,7 +180,12 @@ namespace Discord.WebSocket
if (_shards != null)
{
for (int i = 0; i < _shards.Length; i++)
+ {
+ // Reset the gateway URL set for the shard.
+ _shards[i].ApiClient.GatewayUrl = null;
+
await _shards[i].LogoutAsync();
+ }
}
if (_automaticShards)
diff --git a/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs b/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
index 15d197a55..465c47a1d 100644
--- a/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
+++ b/src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
@@ -38,6 +38,24 @@ namespace Discord.API
public ConnectionState ConnectionState { get; private set; }
+ ///
+ /// Sets the gateway URL used for identifies.
+ ///
+ ///
+ /// If a custom URL is set, setting this property does nothing.
+ ///
+ public string GatewayUrl
+ {
+ set
+ {
+ // Makes the sharded client not override the custom value.
+ if (_isExplicitUrl)
+ return;
+
+ _gatewayUrl = FormatGatewayUrl(value);
+ }
+ }
+
///
/// Sets the gateway URL used for resumes.
///
@@ -214,16 +232,13 @@ namespace Discord.API
string gatewayUrl;
if (_resumeGatewayUrl == null)
{
- if (!_isExplicitUrl)
+ if (!_isExplicitUrl && _gatewayUrl == null)
{
- // TODO: pass this down from DiscordShardedClient, so that it's not requested separately for every single shard
var gatewayResponse = await GetBotGatewayAsync().ConfigureAwait(false);
- gatewayUrl = _gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
- }
- else
- {
- gatewayUrl = _gatewayUrl;
+ _gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
}
+
+ gatewayUrl = _gatewayUrl;
}
else
{