private void OnMessage(object sender, WebSocketMessage e)
        {
            var webSocket = (BitfinexWebSocketWrapper)sender;

            try
            {
                var token = JToken.Parse(e.Message);

                webSocket.ConnectionHandler.KeepAlive(DateTime.UtcNow);

                if (token is JArray)
                {
                    var channel = token[0].ToObject <int>();

                    if (token[1].Type == JTokenType.String)
                    {
                        var type = token[1].Value <string>();

                        switch (type)
                        {
                        // heartbeat
                        case "hb":
                            return;

                        // trade execution
                        case "te":
                            OnUpdate(webSocket, channel, token[2].ToObject <string[]>());
                            break;

                        // ignored -- trades already handled in "te" message
                        // https://github.com/bitfinexcom/bitfinex-api-node#te-vs-tu-messages
                        case "tu":
                            break;

                        default:
                            Log.Trace($"BitfinexSubscriptionManager.OnMessage(): Unexpected message type: {type}");
                            return;
                        }
                    }

                    // public channels
                    else if (channel != 0 && token[1].Type == JTokenType.Array)
                    {
                        if (token[1][0].Type == JTokenType.Array)
                        {
                            OnSnapshot(
                                webSocket,
                                channel,
                                token[1].ToObject <string[][]>()
                                );
                        }
                        else
                        {
                            // pass channel id as separate arg
                            OnUpdate(
                                webSocket,
                                channel,
                                token[1].ToObject <string[]>()
                                );
                        }
                    }
                }
                else if (token is JObject)
                {
                    var raw = token.ToObject <BaseMessage>();
                    switch (raw.Event.ToLowerInvariant())
                    {
                    case "subscribed":
                        OnSubscribe(webSocket, token.ToObject <ChannelSubscription>());
                        return;

                    case "unsubscribed":
                        OnUnsubscribe(webSocket, token.ToObject <ChannelUnsubscribing>());
                        return;

                    case "auth":
                    case "info":
                    case "ping":
                        return;

                    case "error":
                        var error = token.ToObject <ErrorMessage>();
                        // 10300 Subscription failed (generic) | 10301 : Already subscribed | 10302 : Unknown channel
                        // see https://docs.bitfinex.com/docs/ws-general
                        if (error.Code == 10300 || error.Code == 10301 || error.Code == 10302)
                        {
                            _subscribeErrorCode = error.Code;
                            _onSubscribeEvent.Set();
                        }
                        Log.Error($"BitfinexSubscriptionManager.OnMessage(): {e.Message}");
                        return;

                    default:
                        Log.Trace($"BitfinexSubscriptionManager.OnMessage(): Unexpected message format: {e.Message}");
                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                _brokerage.OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, -1, $"Parsing wss message failed. Data: {e.Message} Exception: {exception}"));
                throw;
            }
        }
Beispiel #2
0
        private void OnMessage(object sender, WebSocketMessage e)
        {
            var webSocket = (BitfinexWebSocketWrapper)sender;

            try
            {
                var token = JToken.Parse(e.Message);

                if (token is JArray)
                {
                    var channel = token[0].ToObject <int>();
                    // heartbeat
                    if (token[1].Type == JTokenType.String && token[1].Value <string>() == "hb")
                    {
                        webSocket.ConnectionHandler.KeepAlive(DateTime.UtcNow);
                        return;
                    }

                    // public channels
                    if (channel != 0)
                    {
                        webSocket.ConnectionHandler.KeepAlive(DateTime.UtcNow);

                        if (token.Count() == 2)
                        {
                            OnSnapshot(
                                webSocket,
                                token[0].ToObject <string>(),
                                token[1].ToObject <string[][]>()
                                );
                        }
                        else
                        {
                            // pass channel id as separate arg
                            OnUpdate(
                                webSocket,
                                token[0].ToObject <string>(),
                                token.ToObject <string[]>().Skip(1).ToArray()
                                );
                        }
                    }
                }
                else if (token is JObject)
                {
                    var raw = token.ToObject <Messages.BaseMessage>();
                    switch (raw.Event.ToLowerInvariant())
                    {
                    case "subscribed":
                        OnSubscribe(webSocket, token.ToObject <Messages.ChannelSubscription>());
                        return;

                    case "unsubscribed":
                        OnUnsubscribe(webSocket, token.ToObject <Messages.ChannelUnsubscribing>());
                        return;

                    case "auth":
                    case "info":
                    case "ping":
                        return;

                    case "error":
                        Log.Error($"BitfinexSubscriptionManager.OnMessage(): {e.Message}");
                        return;

                    default:
                        Log.Trace($"BitfinexSubscriptionManager.OnMessage(): Unexpected message format: {e.Message}");
                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                _brokerage.OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, -1, $"Parsing wss message failed. Data: {e.Message} Exception: {exception}"));
                throw;
            }
        }