Esempio n. 1
0
        private async Task <CallResult <SocketSubscription> > CreateAndConnectSocket(bool authenticate, bool sub, Action <JToken[]> onMessage)
        {
            var socket       = CreateSocket(BaseAddress);
            var subscription = new SocketSubscription(socket);

            if (sub)
            {
                subscription.MessageHandlers.Add(DataHandlerName, (subs, data) => DataHandlerSubscription(data, onMessage));
                subscription.AddEvent(SubscriptionEvent);
            }
            else
            {
                subscription.MessageHandlers.Add(DataHandlerName, (subs, data) => DataHandlerQuery(subs, data, onMessage));
                subscription.AddEvent(DataEvent);
            }

            subscription.MessageHandlers.Add(AuthenticationHandlerName, AuthenticationHandler);
            subscription.MessageHandlers.Add(SubscriptionHandlerName, SubscriptionHandler);

            if (authenticate)
            {
                subscription.AddEvent(AuthenticationEvent);
            }

            var connectResult = await ConnectSocket(subscription).ConfigureAwait(false);

            if (!connectResult.Success)
            {
                return(new CallResult <SocketSubscription>(null, connectResult.Error));
            }

            if (authenticate)
            {
                var authResult = await Authenticate(subscription).ConfigureAwait(false);

                if (!authResult.Success)
                {
                    return(new CallResult <SocketSubscription>(null, authResult.Error));
                }
            }

            return(new CallResult <SocketSubscription>(subscription, null));
        }
Esempio n. 2
0
        private async Task <CallResult <SocketSubscription> > CreateAndConnectSocket <T>(bool authenticate, bool sub, Action <T> onMessage) where T : class
        {
            var socket       = CreateSocket(authenticate ? baseAddressAuthenticated: BaseAddress);
            var subscription = new SocketSubscription(socket);

            if (authenticate)
            {
                subscription.MessageHandlers.Add(DataHandlerName, (subs, data) => DataHandlerV2(subs, data, onMessage));
                subscription.MessageHandlers.Add(PingHandlerName, PingHandlerV2);
                subscription.MessageHandlers.Add(SubscriptionHandlerName, SubscriptionHandlerV2);
                subscription.MessageHandlers.Add(AuthenticationHandlerName, AuthenticationHandler);

                subscription.AddEvent(AuthenticationEvent);
            }
            else
            {
                subscription.MessageHandlers.Add(DataHandlerName, (subs, data) => DataHandlerV1(subs, data, onMessage));
                subscription.MessageHandlers.Add(PingHandlerName, PingHandlerV1);
                subscription.MessageHandlers.Add(SubscriptionHandlerName, SubscriptionHandlerV1);
            }

            subscription.AddEvent(sub ? SubscriptionEvent: DataEvent);

            var connectResult = await ConnectSocket(subscription).ConfigureAwait(false);

            if (!connectResult.Success)
            {
                return(new CallResult <SocketSubscription>(null, connectResult.Error));
            }

            if (authenticate)
            {
                var authResult = await Authenticate(subscription).ConfigureAwait(false);

                if (!authResult.Success)
                {
                    return(new CallResult <SocketSubscription>(null, authResult.Error));
                }
            }

            return(new CallResult <SocketSubscription>(subscription, null));
        }