public BitfinexSocketMessagesBase Unsubscribe(int channelId) { //look for the previous subscription BitfinexEventRegistration registration; lock (eventListLock) { registration = eventRegistrations.FirstOrDefault(r => r.ChannelId == channelId && r.Status == EventStatusEnum.Subscribed); } if (registration == null) { //no subscribtion found throw new ArgumentException($"No subscription found for {channelId}"); } //prepare request message var request = new BitfinexUnsubscribeRequest(channelId); var data = JsonConvert.SerializeObject(request, Formatting.Indented); Socket.Send(data); //wait response var response = (BitfinexApiSubscriptionResponse)WaitUnSubscription(registration); return(response); }
/// <inheritdoc /> protected override async Task <bool> Unsubscribe(SocketConnection connection, SocketSubscription subscription) { var channelId = ((BitfinexSubscriptionRequest)subscription.Request !).ChannelId; var unsub = new BitfinexUnsubscribeRequest(channelId); var result = false; await connection.SendAndWait(unsub, ResponseTimeout, data => { if (data.Type != JTokenType.Object) { return(false); } var evnt = (string)data["event"]; var channel = (string)data["chanId"]; if (!int.TryParse(channel, out var chan)) { return(false); } result = evnt == "unsubscribed" && channelId == chan; return(result); }).ConfigureAwait(false); return(result); }
/// <inheritdoc /> protected override async Task <bool> UnsubscribeAsync(SocketConnection connection, SocketSubscription subscription) { if (subscription.Request == null) { // If we don't have a request object we can't unsubscribe it. Probably is an auth subscription which gets pushed regardless // Just returning true here will remove the handler and close the socket if there are no other handlers left on the socket, which is the best we can do return(true); } var channelId = ((BitfinexSubscriptionRequest)subscription.Request !).ChannelId; var unsub = new BitfinexUnsubscribeRequest(channelId); var result = false; await connection.SendAndWaitAsync(unsub, ClientOptions.SocketResponseTimeout, data => { if (data.Type != JTokenType.Object) { return(false); } var evnt = data["event"]?.ToString(); var channel = data["chanId"]?.ToString(); if (evnt == null || channel == null) { return(false); } if (!int.TryParse(channel, out var chan)) { return(false); } result = evnt == "unsubscribed" && channelId == chan; return(result); }).ConfigureAwait(false); return(result); }