Skip to content

Commit

Permalink
Fix to prevent endless looping when reconnecting an authenticated web…
Browse files Browse the repository at this point in the history
…socket
  • Loading branch information
JKorf committed Apr 23, 2024
1 parent 8228a57 commit 6b4c112
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
31 changes: 28 additions & 3 deletions Mexc.Net/Clients/SpotApi/MexcSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,49 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToMiniTickerUpdatesAs
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToAccountUpdatesAsync(string listenKey, Action<DataEvent<MexcAccountUpdate>> handler, CancellationToken ct = default)
{
var subscription = new MexcSubscription<MexcAccountUpdate>(_logger, new[] { "[email protected]" }, handler, false);
var subscription = new MexcSubscription<MexcAccountUpdate>(_logger, new[] { "[email protected]" }, handler, true);
return await SubscribeAsync(BaseAddress + "?listenKey=" + listenKey, subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderUpdatesAsync(string listenKey, Action<DataEvent<MexcUserOrderUpdate>> handler, CancellationToken ct = default)
{
var subscription = new MexcSubscription<MexcUserOrderUpdate>(_logger, new[] { "[email protected]" }, handler, false);
var subscription = new MexcSubscription<MexcUserOrderUpdate>(_logger, new[] { "[email protected]" }, handler, true);
return await SubscribeAsync(BaseAddress + "?listenKey=" + listenKey, subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToUserTradeUpdatesAsync(string listenKey, Action<DataEvent<MexcUserTradeUpdate>> handler, CancellationToken ct = default)
{
var subscription = new MexcSubscription<MexcUserTradeUpdate>(_logger, new[] { "[email protected]" }, handler, false);
var subscription = new MexcSubscription<MexcUserTradeUpdate>(_logger, new[] { "[email protected]" }, handler, true);
return await SubscribeAsync(BaseAddress + "?listenKey=" + listenKey, subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
protected override async Task<Uri?> GetReconnectUriAsync(SocketConnection connection)
{
if (connection.Subscriptions.Any(s => s.Authenticated))
{
// If any of the subs on the connection is authenticated we request a new listenkey
// to prevent endlessly looping if the listenkey happens to be expired
var creds = ApiOptions.ApiCredentials ?? ClientOptions.ApiCredentials;
var client = new MexcRestClient(opts =>
{
if (creds != null)
opts.ApiCredentials = creds;
});

var listenKeyResult = await client.SpotApi.Account.StartUserStreamAsync().ConfigureAwait(false);
if (listenKeyResult)
return new Uri(BaseAddress + "?listenKey=" + listenKeyResult.Data);
}

return await base.GetReconnectUriAsync(connection).ConfigureAwait(false);
}

/// <inheritdoc />
protected override Query? GetAuthenticationRequest() => null;

private static string GetIntervalString(KlineInterval interval)
=> interval switch
{
Expand Down
6 changes: 6 additions & 0 deletions Mexc.Net/Mexc.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b4c112

Please sign in to comment.