aarnott / streamjsonrpc.sample Goto Github PK
View Code? Open in Web Editor NEWSample use of StreamJsonRpc that demonstrate separate client/server processes over named pipes.
Sample use of StreamJsonRpc that demonstrate separate client/server processes over named pipes.
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.
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;
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.