Пример #1
0
        public async Task <dynamic> SendAsync(string method, dynamic args = null)
        {
            var id      = ++_lastId;
            var message = JsonConvert.SerializeObject(new Dictionary <string, object>()
            {
                { "id", id },
                { "method", method },
                { "params", args }
            });

            var encoded = Encoding.UTF8.GetBytes(message);
            var buffer  = new ArraySegment <Byte>(encoded, 0, encoded.Length);

            QueueId(id);

            await _socketQueue.Enqueue(() => WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, default(CancellationToken)));

            //I don't know if this will be the final solution.
            //For now this will prevent the WebSocket from failing after the process is killed by the close method.
            if (method == CloseMessage)
            {
                _closeMessageSent = true;
            }

            return(await _responses[id].Task);
        }
Пример #2
0
        public async Task <dynamic> SendAsync(string method, dynamic args = null)
        {
            var id      = ++_lastId;
            var message = JsonConvert.SerializeObject(new Dictionary <string, object>()
            {
                { "id", id },
                { "method", method },
                { "params", args }
            });

            _responses[id] = new MessageTask
            {
                TaskWrapper = new TaskCompletionSource <dynamic>(),
                Method      = method
            };

            var encoded = Encoding.UTF8.GetBytes(message);
            var buffer  = new ArraySegment <Byte>(encoded, 0, encoded.Length);
            await _socketQueue.Enqueue(() => WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, default(CancellationToken)));

            if (method == CloseMessage)
            {
                StopReading();
            }

            return(await _responses[id].TaskWrapper.Task);
        }
Пример #3
0
        public async Task <Stream> ScreenshotStreamAsync(ScreenshotOptions options)
        {
            string screenshotType = null;

            if (!string.IsNullOrEmpty(options.Type))
            {
                if (options.Type != "png" && options.Type != "jpeg")
                {
                    throw new ArgumentException($"Unknown options.type {options.Type}");
                }
                screenshotType = options.Type;
            }

            if (string.IsNullOrEmpty(screenshotType))
            {
                screenshotType = "png";
            }

            if (options.Quality.HasValue)
            {
                if (screenshotType == "jpeg")
                {
                    throw new ArgumentException($"options.Quality is unsupported for the {screenshotType} screenshots");
                }

                if (options.Quality < 0 || options.Quality > 100)
                {
                    throw new ArgumentException($"Expected options.quality to be between 0 and 100 (inclusive), got {options.Quality}");
                }
            }

            if (options.Clip != null && options.FullPage)
            {
                throw new ArgumentException("options.clip and options.fullPage are exclusive");
            }

            return(await _screenshotTaskQueue.Enqueue(() => PerformScreenshot(screenshotType, options)));
        }
Пример #4
0
 private async void Transport_MessageReceived(object sender, MessageReceivedEventArgs e) => await _callbackQueue.Enqueue(() => ProcessMessage(e));