示例#1
0
        private void MessageReceived(string s, WebSocketClient client)
        {
            JObject jResponse = JObject.Parse(s);

            // if the response does not have an id field, it is not a RippleResponse.
            // it is most likely a stream from the subscribe API
            if (jResponse["id"] == null)
            {
                if (!jResponse.HasValues)
                {
                    return;
                }

                // if there is no active subscriptionStream
                // disregard the message
                if (this.subscriptionStream == null)
                {
                    return;
                }

                this.subscriptionStream.Push(this.ConvertJObjectToDictionary(jResponse));

                return;
            }

            RippleResponse response = jResponse.ToObject <RippleResponse>();

            var taskInfoResult = tasks.TryGetValue(response.Id, out var taskInfo);

            if (taskInfoResult == false)
            {
                throw new Exception("Task not found");
            }

            if (response.Status == "success")
            {
                if (taskInfo.Type != typeof(SubscriptionStream))
                {
                    if (taskInfo.IsUnsubscribe)
                    {
                        this.subscriptionStream = null;
                    }

                    object deserialized = JsonConvert.DeserializeObject(response.Result.ToString(), taskInfo.Type, serializerSettings);

                    MethodInfo setResult = taskInfo.TaskCompletionResult.GetType().GetMethod("SetResult");
                    setResult.Invoke(taskInfo.TaskCompletionResult, new[] { deserialized });
                }
                else
                {
                    TaskCompletionSource <SubscriptionStream> taskCompletionSource = taskInfo.TaskCompletionResult as TaskCompletionSource <SubscriptionStream>;
                    this.subscriptionStream = taskCompletionSource.Task.Result;

                    // since subscriptionStream is not null, MessageReceived will push the first subscription to the SubscriptionStream
                    this.MessageReceived(response.Result.ToString(), client);
                }

                if (taskInfo.RemoveUponCompletion)
                {
                    tasks.TryRemove(response.Id, out taskInfo);
                }
            }
            else if (response.Status == "error")
            {
                var setException = taskInfo.TaskCompletionResult.GetType().GetMethod("SetException", new Type[] { typeof(Exception) }, null);

                RippleException exception = new RippleException(response.Error);
                setException.Invoke(taskInfo.TaskCompletionResult, new[] { exception });

                tasks.TryRemove(response.Id, out taskInfo);
            }
        }
示例#2
0
 private void Error(Exception ex, WebSocketClient client)
 {
     throw new Exception(ex.Message, ex);
 }