示例#1
0
        public async Task Subscribe <TNotificationData>(ISocketSubscriptionRequest socketRequest, Action <TNotificationData> callback) where TNotificationData : class
        {
            await Connect();

            var socketAction = new SocketSubscriptionAction(socketRequest);

            socketAction.ResponseReceived += (o, e) =>
            {
                var socketNotification = ApiExtensions.DecodeSocketNotification <TNotificationData>(e.Message);
                if (socketNotification.ErrorData != null)
                {
                    throw new ConnectorException($"{socketNotification.ErrorData.Message}. {socketNotification.ErrorData.Description}");
                }
                callback(socketNotification.NotificationParameters);
            };
            RunAction(socketAction);
        }
示例#2
0
        public async Task <TResponseResult> DoRequest <TResponseResult>(ISingleSocketRequest socketRequest, Action errorCallback = null) where TResponseResult : class
        {
            await Connect();

            SocketResponse <TResponseResult> socketResponse = null;
            var responseReceived = false;

            socketRequest.Id = _idGenerator.CreateId();
            var socketAction = new SingleSocketAction(socketRequest);

            socketAction.ResponseReceived += (o, e) =>
            {
                socketResponse   = ApiExtensions.DecodeSocketResponse <TResponseResult>(e.Message);
                responseReceived = true;
            };
            socketAction.ErrorReceived += (o, e) =>
            {
                errorCallback?.Invoke();
            };

            RunAction(socketAction);

            if (!socketRequest.NeedResponse)
            {
                return(Activator.CreateInstance <TResponseResult>());
            }

            while (!responseReceived)
            {
                await Task.Delay(50);
            }

            if (socketResponse?.ErrorData != null)
            {
                throw new ConnectorException($"{socketResponse?.ErrorData.Message}. {socketResponse?.ErrorData.Description}");
            }

            return(socketResponse?.ResponseData);
        }