EleCho.JsonRpc 2.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package EleCho.JsonRpc --version 2.0.0                
NuGet\Install-Package EleCho.JsonRpc -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EleCho.JsonRpc" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EleCho.JsonRpc --version 2.0.0                
#r "nuget: EleCho.JsonRpc, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EleCho.JsonRpc as a Cake Addin
#addin nuget:?package=EleCho.JsonRpc&version=2.0.0

// Install EleCho.JsonRpc as a Cake Tool
#tool nuget:?package=EleCho.JsonRpc&version=2.0.0                

EleCho.JsonRpc alternate text is missing from this package README image alternate text is missing from this package README image

基于 JSON 的简单 RPC 库.
Simple JSON based RPC library.

通过阅读此项目的代码, 你可以学到: 动态代理.
By reading the code of this project, you can learn: Dynamic proxy.

传输 / Transmission

--> {"jsonrpc":"2.0","method":"方法名","signature":"方法签名","params":["参数"],"id":"ID"}
<-- {"jsonrpc":"2.0","result":"返回值","id":"ID"}
--> {"jsonrpc":"2.0","method":"method name","signature":"method signature","params":["parameter"],"id":"ID"}
<-- {"jsonrpc":"2.0","result":"return value","id":"ID"}

规范: JSON-RPC 2.0 规范
Specification: JSON-RPC 2.0 Specification

功能 / Features

  • 基本功能(Basic features)
  • 异步方法(Async methods)
  • Ref/Out 参数(Ref/Out parameters)
  • 批量请求(Batch request)

使用 / Usage

该库可以在 System.IO.Stream 上使用
This library can be used on System.IO.Stream

定义公共的接口(Define the public interface):

public interface Commands
{
    public void WriteLine(string message);
    public int Add(int a, int b);
    public int Add114514(ref int num);
}

服务端对接口的实现(Server implementation of the interface):

internal class CommandsImpl : Commands
{
    public int Add(int a, int b) => a + b;
    public int Add114514(ref int num) => num += 114514;
    public void WriteLine(string message) => Console.WriteLine("Server print: " + message);
}

服务端监听 TCP (Server listening on TCP):

int port = 11451;

TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));      // 监听指定端口 / listen on specified port
listener.Start();

CommandsImpl serverCommands = new CommandsImpl();                                 // 创建公用的指令调用实例 / Create a common command call instance
List<RpcServer<Commands>> rpcs = new List<RpcServer<Commands>>();                 // 保存所有客户端 RPC 引用 / Save all client RPC references

Console.WriteLine($"Listening {port}");

while (true)
{
    TcpClient client = await listener.AcceptTcpClientAsync();                     // 接受一个客户端 / Accept a client
    rpcs.Add(new RpcServer<Commands>(client.GetStream(), serverCommands));        // 创建并保存 RPC 实例 / Create and save an RPC instance
}

客户端连接并调用远程函数(The client connects and calls the remote function):

Console.Write("Addr: ");
var addr = Console.ReadLine()!;                         // 用户输入地址 / User enters the address

TcpClient client = new TcpClient();
client.Connect(IPEndPoint.Parse(addr));                 // 连接到服务器 / Connect to server

RpcClient<Commands> rpc =
    new RpcClient<Commands>(client.GetStream());        // 创建 RPC 客户端实例 / Create an RPC client instance

int num = 10;
rpc.Remote.Add114514(ref num);

if (num == 114524)
    Console.WriteLine("带 ref 参数的 RPC 调用成功");

while (true)
{
    var input = Console.ReadLine();
    if (input == null)
        break;

    rpc.Remote.WriteLine(input);                        // 调用服务端 WriteLine 方法 / Call the server WriteLine method
}

客户端控制台(Client console):
Addr: 127.0.0.1:11451
带 ref 参数的 RPC 调用成功
hello
this message is from client

服务端控制台:
Listening 11451
Server print: hello
Server print: this message is from client

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 107 5/21/2024
2.0.0 94 5/21/2024
1.1.3 467 1/29/2023
1.1.2 317 1/11/2023
1.1.1 315 1/11/2023
1.1.0 311 1/11/2023
1.0.0 297 1/10/2023