Ejemplo n.º 1
0
        internal void OnMessage(string message)
        {
            dynamic obj          = JsonConvert.DeserializeObject(message);
            var     objAsJObject = (JObject)obj;

            _logger.LogTrace("◀ Receive {Message}", message);

            var id = (int?)objAsJObject["id"];

            if (id.HasValue && _callbacks.TryGetValue(id.Value, out var callback) && _callbacks.Remove(id.Value))
            {
                if (objAsJObject["error"] != null)
                {
                    callback.TaskWrapper.TrySetException(new MessageException(callback, obj));
                }
                else
                {
                    if (callback.RawContent)
                    {
                        callback.TaskWrapper.TrySetResult(JsonConvert.SerializeObject(obj.result));
                    }
                    else
                    {
                        callback.TaskWrapper.TrySetResult(obj.result);
                    }
                }
            }
            else
            {
                if (obj.method == "Tracing.tracingComplete")
                {
                    TracingComplete?.Invoke(this, new TracingCompleteEventArgs
                    {
                        Stream = objAsJObject["params"].Value <string>("stream")
                    });
                }
                else if (obj.method == "Target.receivedMessageFromTarget")
                {
                    var sessionId = objAsJObject["params"]["sessionId"].ToString();
                    if (_sessions.TryGetValue(sessionId, out var session))
                    {
                        session.OnMessage(objAsJObject["params"]["message"].ToString());
                    }
                }
                else if (obj.method == "Target.detachedFromTarget")
                {
                    var sessionId = objAsJObject["params"]["sessionId"].ToString();
                    if (_sessions.TryGetValue(sessionId, out var session) && _sessions.Remove(sessionId))
                    {
                        session.OnClosed();
                    }
                }

                MessageReceived?.Invoke(this, new MessageEventArgs
                {
                    MessageID   = obj.method,
                    MessageData = objAsJObject["params"]
                });
            }
        }
Ejemplo n.º 2
0
        internal void OnMessage(string message)
        {
            dynamic obj          = JsonConvert.DeserializeObject(message);
            var     objAsJObject = obj as JObject;

            _logger.LogTrace("◀ Receive {Message}", message);

            if (objAsJObject["id"] != null && _callbacks.ContainsKey((int)obj.id))
            {
                var callback = _callbacks[(int)obj.id];
                _callbacks.Remove((int)obj.id);

                if (objAsJObject["error"] != null)
                {
                    callback.TaskWrapper.SetException(new MessageException(
                                                          $"Protocol error ({ callback.Method }): {obj.error.message} {obj.error.data}"
                                                          ));
                }
                else
                {
                    if (callback.RawContent)
                    {
                        callback.TaskWrapper.SetResult(JsonConvert.SerializeObject(obj.result));
                    }
                    else
                    {
                        callback.TaskWrapper.SetResult(obj.result);
                    }
                }
            }
            else if (obj.method == "Tracing.tracingComplete")
            {
                TracingComplete?.Invoke(this, new TracingCompleteEventArgs
                {
                    Stream = objAsJObject["params"].Value <string>("stream")
                });
            }
            else
            {
                MessageReceived?.Invoke(this, new MessageEventArgs
                {
                    MessageID   = obj.method,
                    MessageData = objAsJObject["params"]
                });
            }
        }
Ejemplo n.º 3
0
        internal void OnMessage(string message)
        {
            _logger.LogTrace("◀ Receive {Message}", message);

            JObject obj = null;

            try
            {
                obj = JObject.Parse(message);
            }
            catch (JsonException exc)
            {
                _logger.LogError(exc, "Failed to deserialize message", message);
                return;
            }

            var id = obj[MessageKeys.Id]?.Value <int>();

            if (id.HasValue && _callbacks.TryRemove(id.Value, out var callback))
            {
                if (obj[MessageKeys.Error] != null)
                {
                    callback.TaskWrapper.TrySetException(new MessageException(callback, obj));
                }
                else
                {
                    callback.TaskWrapper.TrySetResult(obj[MessageKeys.Result].Value <JObject>());
                }
            }
            else
            {
                var method = obj[MessageKeys.Method].AsString();
                var param  = obj[MessageKeys.Params];

                if (method == "Tracing.tracingComplete")
                {
                    TracingComplete?.Invoke(this, new TracingCompleteEventArgs
                    {
                        Stream = param[MessageKeys.Stream].AsString()
                    });
                }
                else if (method == "Target.receivedMessageFromTarget")
                {
                    var sessionId = param[MessageKeys.SessionId].AsString();

                    if (_sessions.TryGetValue(sessionId, out var session))
                    {
                        session.OnMessage(param[MessageKeys.Message].AsString());
                    }
                }
                else if (method == "Target.detachedFromTarget")
                {
                    var sessionId = param[MessageKeys.SessionId].AsString();

                    if (_sessions.TryRemove(sessionId, out var session))
                    {
                        session.Close("Target.detachedFromTarget");
                    }
                }

                MessageReceived?.Invoke(this, new MessageEventArgs
                {
                    MessageID   = method,
                    MessageData = param
                });
            }
        }
Ejemplo n.º 4
0
        internal void OnMessage(string message)
        {
            _logger.LogTrace("◀ Receive {Message}", message);

            ConnectionResponse obj = null;

            try
            {
                obj = JsonConvert.DeserializeObject <ConnectionResponse>(message, JsonHelper.DefaultJsonSerializerSettings);
            }
            catch (JsonException exc)
            {
                _logger.LogError(exc, "Failed to deserialize message", message);
                return;
            }

            var id = obj.Id;

            if (id.HasValue && _callbacks.TryRemove(id.Value, out var callback))
            {
                if (obj.Error != null)
                {
                    callback.TaskWrapper.TrySetException(new MessageException(callback, obj.Error));
                }
                else
                {
                    callback.TaskWrapper.TrySetResult(obj.Result);
                }
            }
            else
            {
                var method = obj.Method;
                var param  = obj.Params?.ToObject <ConnectionResponseParams>();

                if (method == "Tracing.tracingComplete")
                {
                    TracingComplete?.Invoke(this, new TracingCompleteEventArgs
                    {
                        Stream = param.Stream
                    });
                }
                else if (method == "Target.receivedMessageFromTarget")
                {
                    var sessionId = param.SessionId;

                    if (_sessions.TryGetValue(sessionId, out var session))
                    {
                        session.OnMessage(param.Message);
                    }
                }
                else if (method == "Target.detachedFromTarget")
                {
                    var sessionId = param.SessionId;

                    if (_sessions.TryRemove(sessionId, out var session))
                    {
                        session.Close("Target.detachedFromTarget");
                    }
                }

                MessageReceived?.Invoke(this, new MessageEventArgs
                {
                    MessageID   = method,
                    MessageData = obj.Params
                });
            }
        }