static async Task MainAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("Connecting to web socket...");
            using (var socket = new ClientWebSocket())
            {
                await socket.ConnectAsync(new Uri("wss://localhost:44392/socket"), cancellationToken);

                Console.WriteLine("Connected to web socket. Establishing JSON-RPC protocol...");
                using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(socket)))
                {
                    try
                    {
                        jsonRpc.AddLocalRpcMethod("Tick", new Action <int>(tick => Console.WriteLine($"Tick {tick}!")));
                        jsonRpc.StartListening();
                        Console.WriteLine("JSON-RPC protocol over web socket established.");
                        int result = await jsonRpc.InvokeWithCancellationAsync <int>("Add", new object[] { 1, 2 }, cancellationToken);

                        Console.WriteLine($"JSON-RPC server says 1 + 2 = {result}");

                        // Request notifications from the server.
                        await jsonRpc.NotifyAsync("SendTicksAsync");

                        await jsonRpc.Completion.WithCancellation(cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        // Closing is initiated by Ctrl+C on the client.
                        // Close the web socket gracefully -- before JsonRpc is disposed to avoid the socket going into an aborted state.
                        await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client closing", CancellationToken.None);

                        throw;
                    }
                }
            }
        }
Пример #2
0
        public void StopCommand()
        {
            if (rpc == null)
            {
                return;
            }

            try {
                rpc.NotifyAsync(Methods.StopCommandName).Ignore();
            } catch (Exception ex) {
                LoggingService.LogError("StopCommand error", ex);
            }
        }
        private void Buffer_ChangedLowPriority(object sender, TextContentChangedEventArgs e)
        {
            if (
                rpc is null ||
                !(sender is ITextBuffer buffer) ||
                !buffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDocument) ||
                textDocument is null)
            {
                return;
            }

            var before = e.Before;

            var changes = e.Changes.Select(change => ConvertChange(before, change)).ToList();
            var arg     = new { uri = new Uri(textDocument.FilePath).AbsoluteUri, changes };

            _ = rpc.NotifyAsync("$/onDidChangeTsOrJsFile", arg);
        }
Пример #4
0
    public async Task SendMessageWithEncoding(string encodingName)
    {
        var messageHandler = new HeaderDelimitedMessageHandler(this.clientStream, this.clientStream);
        var rpcClient      = new JsonRpc(messageHandler);

        messageHandler.Encoding = Encoding.GetEncoding(encodingName);
        await rpcClient.NotifyAsync("Foo").WithCancellation(this.TimeoutToken);

        rpcClient.Dispose();

        MemoryStream seekableServerStream = await this.GetSeekableServerStream();

        int    bytesRead   = 0;
        var    reader      = new StreamReader(seekableServerStream, Encoding.ASCII);
        var    headerLines = new List <string>();
        string?line;

        while ((line = await reader.ReadLineAsync().WithCancellation(this.TimeoutToken)) != string.Empty)
        {
            Assumes.NotNull(line);
            headerLines.Add(line);
            bytesRead += line.Length + 2; // + CRLF
        }

        bytesRead += 2; // final CRLF
        this.Logger.WriteLine(string.Join(Environment.NewLine, headerLines));

        // utf-8 headers may not be present because they are the default, per the protocol spec.
        if (encodingName != "utf-8")
        {
            Assert.Contains(headerLines, l => l.Contains($"charset={encodingName}"));
        }

        // Because the first StreamReader probably read farther (to fill its buffer) than the end of the headers,
        // we need to reposition the stream at the start of the content to create a new StreamReader.
        seekableServerStream.Position = bytesRead;
        reader = new StreamReader(seekableServerStream, Encoding.GetEncoding(encodingName));
        string json = await reader.ReadToEndAsync().WithCancellation(this.TimeoutToken);

        Assert.Equal('{', json[0]);
    }
Пример #5
0
        static async Task MainAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("Connecting to web socket...");
            using (var socket = new ClientWebSocket())
            {
                await socket.ConnectAsync(new Uri("ws://localhost:7919"), cancellationToken);

                Console.WriteLine("Connected to web socket. Establishing JSON-RPC protocol...");

                // to work with 'vscode-jsonrpc' server, need to use this handler
                // refer to: https://github.com/microsoft/vs-streamjsonrpc/blob/main/doc/extensibility.md
                var handler = new HeaderDelimitedMessageHandler(socket.AsStream());
                var jsonRpc = new JsonRpc(handler);
                try
                {
                    jsonRpc.StartListening();
                    Console.WriteLine("JSON-RPC protocol over web socket established.");

                    await jsonRpc.NotifyAsync("testNotification", "Hello from dotnet");

                    object[] param  = { 1, 2 };
                    var      result = await jsonRpc.InvokeWithCancellationAsync <int>("Add", param, cancellationToken);

                    Console.WriteLine($"JSON-RPC server says 1 + 2 = {result}");

                    await jsonRpc.Completion.WithCancellation(cancellationToken);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client Closing", CancellationToken.None);

                    throw;
                }
            }
        }
Пример #6
0
 public Task Report(string message) => _rpc.NotifyAsync("python/reportProgress", message);
Пример #7
0
 public Progress(JsonRpc rpc)
 {
     _rpc = rpc;
     _rpc.NotifyAsync("python/beginProgress").DoNotWait();
 }
 public async ValueTask SendNotificationAsync(string methodName, CancellationToken cancellationToken)
 => await _jsonRpc.NotifyAsync(methodName).ConfigureAwait(false);
 public Task NotifyAsync(string targetName, params object[] arguments)
 => arguments != null && arguments.Length > 0
         ?  _rpc.NotifyAsync(targetName, arguments)
         :  _rpc.NotifyAsync(targetName);