示例#1
0
        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);
            }
        }
示例#2
0
        private void OnProcess(dynamic obj)
        {
            var channel = (string)obj.channel;
            var evt     = (string)obj.@event;
            var data    = obj.data;

            //if (data != null && evt != "bts:error")
            //	data = ((string)data).DeserializeObject<object>();

            switch (evt)
            {
            //case "pusher:connection_established":
            //	_activityTimeout = (int)data.activity_timeout;
            //	_nextPing = DateTime.UtcNow.AddSeconds(_activityTimeout);
            //	Connected?.Invoke();
            //	break;

            case "bts:error":
                Error?.Invoke(new InvalidOperationException((string)data.message));
                break;

            case "ping":
                SendPingPong("pong");
                break;

            case "pong":
                break;

            case "bts:subscription_succeeded":
            {
                if (channel.StartsWith(ChannelNames.OrderBook))
                {
                    OrderBookSubscribed?.Invoke(GetPair(channel, ChannelNames.OrderBook));
                }
                else if (channel.StartsWith(ChannelNames.Trades))
                {
                    TradesSubscribed?.Invoke(GetPair(channel, ChannelNames.Trades));
                }
                else if (channel.StartsWith(ChannelNames.OrderLog))
                {
                    OrderLogSubscribed?.Invoke(GetPair(channel, ChannelNames.OrderLog));
                }
                else
                {
                    this.AddErrorLog(LocalizedStrings.Str3311Params, channel);
                }

                break;
            }

            case "bts:unsubscription_succeeded":
            {
                if (channel.StartsWith(ChannelNames.OrderBook))
                {
                    OrderBookUnSubscribed?.Invoke(GetPair(channel, ChannelNames.OrderBook));
                }
                else if (channel.StartsWith(ChannelNames.Trades))
                {
                    TradesUnSubscribed?.Invoke(GetPair(channel, ChannelNames.Trades));
                }
                else if (channel.StartsWith(ChannelNames.OrderLog))
                {
                    OrderLogUnSubscribed?.Invoke(GetPair(channel, ChannelNames.OrderLog));
                }
                else
                {
                    this.AddErrorLog(LocalizedStrings.Str3311Params, channel);
                }

                break;
            }

            case "trade":
                NewTrade?.Invoke(GetPair(channel, ChannelNames.Trades), ((JToken)data).DeserializeObject <Trade>());
                break;

            case "data":
                NewOrderBook?.Invoke(GetPair(channel, ChannelNames.OrderBook), ((JToken)data).DeserializeObject <OrderBook>());
                break;

            case "order_created":
            case "order_changed":
            case "order_deleted":
                NewOrderLog?.Invoke(GetPair(channel, ChannelNames.OrderLog), evt == "order_deleted" ? OrderStates.Done : OrderStates.Active, ((JToken)data).DeserializeObject <Order>());
                break;

            default:
                this.AddErrorLog(LocalizedStrings.Str3312Params, evt);
                break;
            }
        }