private void WebSocket_MessageReceived(object sender, MessageReceivedEventArgs e) { Postback <OrderPostBack> postback = Kite.ParseString <Postback <OrderPostBack> >(e.Message, logger: this.logger); if (postback?.data != null) { OnPostback(postback.data); } else { try { this.logger?.OnLog(e.Message); } catch (Exception ex) { } } }
private async void ReceiveAsync() { this.lastUpdateTime = DateTime.UtcNow; if (this.timer == null) { InitializeTimer(); } try { ClientWebSocket tmpSocket = this.socket; if (tmpSocket == null) return; while (tmpSocket.State == WebSocketState.Open || tmpSocket.State == WebSocketState.CloseSent) { WebSocketReceiveResult result; byte[] buffer = new byte[64 * 1024]; int offset = 0; do { byte[] tmpBytes = new byte[64 * 1024]; result = await tmpSocket.ReceiveAsync(new ArraySegment<byte>(tmpBytes), CancellationToken.None).ConfigureAwait(false); int count = result.Count; if ((offset + count) > buffer.Length) { byte[] tmp = buffer; buffer = new byte[buffer.Length + 64 * 1024]; Array.Copy(tmp, buffer, tmp.Length); } Array.Copy(tmpBytes, 0, buffer, offset, count); offset += count; } while (!result.EndOfMessage); if (result.MessageType == WebSocketMessageType.Binary) { byte[] data = new byte[offset]; Array.Copy(buffer, 0, data, 0, offset); OnQuotes(data); } else if (result.MessageType == WebSocketMessageType.Text) { //parse the json msg byte[] data = new byte[offset]; Array.Copy(buffer, 0, data, 0, offset); Postback<OrderPostBack> orderPostback = Kite.ParseBytes<Postback<OrderPostBack>>(data, logger: this.logger); if (orderPostback?.data != null) { OnPostback(orderPostback.data); } try { this.logger?.OnLog($"{Encoding.UTF8.GetString(data)}"); } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } } } else if (result.MessageType == WebSocketMessageType.Close) { try { await tmpSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } } OnState(KiteConnectState.Disconnected); return; } } } catch (Exception ex) { try { this.logger?.OnException(ex); } catch (Exception ex1) { } } }