private void OnReceive() { try { var buf = new byte[1024 * 1024]; var pos = 0; var errorCount = 0; const int maxErrorCount = 10; while (!IsDisposed && _connected) { try { var task = _ws.ReceiveAsync(new ArraySegment <byte>(buf, pos, buf.Length - pos), _source.Token); task.Wait(); var result = task.Result; if (result.CloseStatus != null) { if (task.Exception != null) { PusherError.SafeInvoke(task.Exception); } break; } pos += result.Count; if (!result.EndOfMessage) { continue; } var recv = Encoding.UTF8.GetString(buf, 0, pos); this.AddDebugLog(recv); pos = 0; var obj = JsonConvert.DeserializeObject <ChannelData>(recv); switch (obj.Event) { case "pusher:connection_established": PusherConnected.SafeInvoke(); break; case "pusher_internal:subscription_succeeded": { switch (obj.Channel) { case "live_trades": TradesSubscribed.SafeInvoke(); break; case "order_book": OrderBooksSubscribed.SafeInvoke(); break; default: this.AddErrorLog(LocalizedStrings.Str3311Params, obj.Event); break; } break; } case "trade": NewTrade.SafeInvoke(JsonConvert.DeserializeObject <Trade>(obj.Data)); break; case "data": NewOrderBook.SafeInvoke(JsonConvert.DeserializeObject <OrderBook>(obj.Data)); break; default: this.AddErrorLog(LocalizedStrings.Str3312Params, obj.Event); break; } errorCount = 0; } catch (AggregateException ex) { PusherError.SafeInvoke(ex); var socketError = ex.InnerExceptions.FirstOrDefault() as WebSocketException; if (socketError != null) { break; } if (++errorCount >= maxErrorCount) { this.AddErrorLog("Max error {0} limit reached.", maxErrorCount); break; } } catch (Exception ex) { PusherError.SafeInvoke(ex); } } _ws.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, _source.Token).Wait(); _ws.Dispose(); PusherDisconnected.SafeInvoke(); } catch (Exception ex) { PusherError.SafeInvoke(ex); } }
private void _pusherClient_Error(object sender, PusherException error) { log.Write(LogLevel.Error, error.ToString()); PusherError?.Invoke(error); }