private async Task SubscribeAndListenImplAsync(CancellationToken cancellationToken, string requestString) { await _clientWebSocket.SendAsync(StringToArraySegment(requestString), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false); await Subscribed.NullableInvokeAsync(this, requestString); await ListenToMessagesAsync(_clientWebSocket, cancellationToken); }
public async Task CloseConnectionAsync(CancellationToken cancellationToken) { if (_clientWebSocket != null && _clientWebSocket.State == WebSocketState.Open) { await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal closure", cancellationToken); await Disconnected.NullableInvokeAsync(this, BaseUri); } }
private async Task HandleWebSocketMessageAsync(string jsonMessage) { var jToken = JToken.Parse(jsonMessage); var type = jToken["type"]?.Value <string>(); switch (type) { case "received": var orderReceived = JsonConvert.DeserializeObject <GdaxWssOrderReceived>(jsonMessage); await OrderReceived.NullableInvokeAsync(this, orderReceived); break; case "open": var orderOpen = JsonConvert.DeserializeObject <GdaxWssOrderOpen>(jsonMessage); await OrderOpened.NullableInvokeAsync(this, orderOpen); break; case "done": var orderDone = JsonConvert.DeserializeObject <GdaxWssOrderDone>(jsonMessage); await OrderDone.NullableInvokeAsync(this, orderDone); break; case "match": var orderMatch = JsonConvert.DeserializeObject <GdaxWssOrderMatch>(jsonMessage); await OrderMatched.NullableInvokeAsync(this, orderMatch); break; case "change": var orderChange = JsonConvert.DeserializeObject <GdaxWssOrderChange>(jsonMessage); await OrderChanged.NullableInvokeAsync(this, orderChange); break; case "ticker": var tickerDetails = JsonConvert.DeserializeObject <GdaxWssTicker>(jsonMessage); await Ticker.NullableInvokeAsync(this, tickerDetails); break; case "error": var error = JsonConvert.DeserializeObject <GdaxWssError>(jsonMessage); await Error.NullableInvokeAsync(this, error); break; default: // Clients are expected to ignore messages they do not support. break; } }
public async Task ConnectAsync(CancellationToken cancellationToken) { await _logger.WriteInfoAsync(nameof(GdaxWebSocketApi), nameof(ConnectAsync), BaseUri.ToString(), "Attempt to establish a socket connection"); _clientWebSocket = new ClientWebSocket(); await _clientWebSocket.ConnectAsync(BaseUri, cancellationToken).ConfigureAwait(false); if (_clientWebSocket.State != WebSocketState.Open) { throw new ApiException($"Could not establish WebSockets connection to {BaseUri}"); } await Connected.NullableInvokeAsync(this, BaseUri); }