Skip to content

Commit

Permalink
Fixed socketClient.SpotApiV2.SubscribeToOrderBookUpdatesAsync unsubsc…
Browse files Browse the repository at this point in the history
…ribe
JKorf committed Dec 15, 2024
1 parent f9995de commit fb46ebc
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 3 additions & 3 deletions CoinEx.Net/Clients/SpotApiV2/CoinExSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
@@ -135,10 +135,10 @@ public Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(str
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(IEnumerable<string> symbols, int depth, string? mergeLevel, bool fullBookUpdates, Action<DataEvent<CoinExOrderBook>> onMessage, CancellationToken ct = default)
{
var subscription = new CoinExSubscription<CoinExOrderBook>(_logger, "depth", symbols, new Dictionary<string, object>
var subscription = new CoinExOrderBookSubscription(_logger, symbols, new Dictionary<string, object>
{
{ "market_list", symbols.Select(x => new object[] { x, depth, mergeLevel ?? "0", fullBookUpdates }).ToList() }
}, x => onMessage(x.WithSymbol(x.Data.Symbol)), firstUpdateIsSnapshot: true);
}, x => onMessage(x.WithSymbol(x.Data.Symbol)));
return await SubscribeAsync(BaseAddress.AppendPath("v2/spot"), subscription, ct).ConfigureAwait(false);
}

@@ -156,7 +156,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToTradeUpdatesAsync(I
var subscription = new CoinExTradesSubscription(_logger, symbols, new Dictionary<string, object>
{
{ "market_list", symbols }
}, onMessage);
}, onMessage);
return await SubscribeAsync(BaseAddress.AppendPath("v2/spot"), subscription, ct).ConfigureAwait(false);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using CoinEx.Net.Objects.Models.V2;
using CoinEx.Net.Objects.Sockets.V2.Queries;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Sockets;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CoinEx.Net.Objects.Sockets.V2.Subscriptions
{
internal class CoinExOrderBookSubscription : Subscription<CoinExSocketResponse, CoinExSocketResponse>
{
private IEnumerable<string> _symbols;
private Dictionary<string, object> _parameters;
private Action<DataEvent<CoinExOrderBook>> _handler;

public override HashSet<string> ListenerIdentifiers { get; set; }
public CoinExOrderBookSubscription(ILogger logger, IEnumerable<string> symbols, Dictionary<string, object> parameters, Action<DataEvent<CoinExOrderBook>> handler) : base(logger, false)
{
_symbols = symbols;
_parameters = parameters;
_handler = handler;
ListenerIdentifiers = new HashSet<string>(_symbols.Select(x => "depth.update" + x));
}

public override CallResult DoHandleMessage(SocketConnection connection, DataEvent<object> message)
{
var data = (CoinExSocketUpdate<CoinExOrderBook>)message.Data;
_handler.Invoke(message.As(data.Data, data.Method, data.Data.Symbol, ConnectionInvocations == 1 ? SocketUpdateType.Snapshot : SocketUpdateType.Update));
return new CallResult(null);
}

public override Type? GetMessageType(IMessageAccessor message) => typeof(CoinExSocketUpdate<CoinExOrderBook>);

public override Query? GetSubQuery(SocketConnection connection)
=> new CoinExQuery("depth.subscribe", _parameters, false, 1);

public override Query? GetUnsubQuery()
=> new CoinExQuery("depth.unsubscribe", new Dictionary<string, object> { {"market_list", _symbols } }, false, 1);
}
}

0 comments on commit fb46ebc

Please sign in to comment.