Git Product home page Git Product logo

streamjsonrpc.sample's People

Contributors

aarnott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

streamjsonrpc.sample's Issues

StdIn / Stdout Example

Beside the 'NamedPipeServerStream' I would love to see a jsonrpc sample like your's but based on StandardIO Streams.

Also I would like to see a
public static async Task Main(string[] args)

as the main entry point in your sample.

Not receiving successful responses with NewLineDelimitedMessageHandler

After running the client/server examples successfully, I started working with a modified version of the StreamJsonRpc.Sample and to test things out I am attempting to pipe a JSON RPC message into it like so:

./ConsoleApp1 < example.json

example.json:

{"jsonrpc":"2.0","method":"Add","params":[1,2],"id":1}

example.json (hexdump to show CR LF ending):

┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 7b 22 6a 73 6f 6e 72 70 ┊ 63 22 3a 22 32 2e 30 22 │{"jsonrp┊c":"2.0"│
│00000010│ 2c 22 6d 65 74 68 6f 64 ┊ 22 3a 22 41 64 64 22 2c │,"method┊":"Add",│
│00000020│ 22 70 61 72 61 6d 73 22 ┊ 3a 5b 31 2c 32 5d 2c 22 │"params"┊:[1,2],"│
│00000030│ 69 64 22 3a 31 7d 0d 0a ┊                         │id":1}__┊        │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘

The result is:

Connection request #0 received. Spinning off an async Task to cater to requests.
JSON-RPC listener attached to #0. Waiting for requests...
Received request: 1 + 2
Connection #0 terminated.

The message was received and logged to STDERR but no response was returned to STDOUT.

However, If I supply a payload that results in an error, I receive a response:

./ConsoleApp1 < faulty.json
Connection request #0 received. Spinning off an async Task to cater to requests.
JSON-RPC listener attached to #0. Waiting for requests...
{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"No method by the name 'Nowhere' is found.","data":null}}
Connection #0 terminated.

faulty.json:

{"jsonrpc":"2.0","method":"Nowhere","params":[1,2],"id":1}

faulty.json (hexdump):

┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 7b 22 6a 73 6f 6e 72 70 ┊ 63 22 3a 22 32 2e 30 22 │{"jsonrp┊c":"2.0"│
│00000010│ 2c 22 6d 65 74 68 6f 64 ┊ 22 3a 22 4e 6f 77 68 65 │,"method┊":"Nowhe│
│00000020│ 72 65 22 2c 22 70 61 72 ┊ 61 6d 73 22 3a 5b 31 2c │re","par┊ams":[1,│
│00000030│ 32 5d 2c 22 69 64 22 3a ┊ 31 7d 0d 0a             │2],"id":┊1}__    │
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘

Am I doing something wrong? The modified example I'm using:

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading.Tasks;
using Nerdbank.Streams;
using StreamJsonRpc;

internal class Server
{
    public int Add(int a, int b)
    {
        Console.Error.WriteLine($"Received request: {a} + {b}");
        return a + b;
    }
}

class Program
{
    static async Task<int> Main(string[] args)
    {
        int clientId = 0;

        await Console.Error.WriteLineAsync($"Connection request #{clientId} received. Spinning off an async Task to cater to requests.");

        var nl = new NewLineDelimitedMessageHandler(Console.OpenStandardOutput(), Console.OpenStandardInput(), new JsonMessageFormatter());

        var jsonRpc = new JsonRpc(nl, new Server());

        await Console.Error.WriteLineAsync($"JSON-RPC listener attached to #{clientId}. Waiting for requests...");

        jsonRpc.StartListening();

        await jsonRpc.Completion;

        await Console.Error.WriteLineAsync($"Connection #{clientId} terminated.");

        return 0;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.