JSON-RPC是一个无状态且轻量级的远程过程调用(RPC)协议。 其使用JSON(RFC 4627)作为数据格式,它的调用与语言,平台无关,且可以直接应用于Web等。
安装RRQMSocket.RPC.JsonRpc
即可,具体步骤详看链接博客。
VS、Unity安装和使用Nuget包
新建类文件,命名为Server,继承自ServerProvider,然后添加一个方法,使用【JsonRpc】标记。
public class Server: ServerProvider
{
[JsonRpc]
public string TestJsonRpc(string str)
{
return "RRQM";
}
}
static IRPCParser CreateTcpJsonRpcParser()
{
JsonRpcParser jsonRpcParser = new JsonRpcParser();
JsonRpcParserConfig config = new JsonRpcParserConfig();
config.ProtocolType = JsonRpcProtocolType.Tcp;//使用Tcp协议,调用时,有且仅有调用消息末尾追加“\r\n”。否则会调用失败。
config.ListenIPHosts = new RRQMSocket.IPHost[] { new RRQMSocket.IPHost(7705)};
config.ProxyToken = "RPC";//生成代理时需要验证
jsonRpcParser.Setup(config);
jsonRpcParser.Start();
Console.WriteLine("TCP协议的JsonRpc已启动");
return jsonRpcParser;
}
static IRPCParser CreateHTTPJsonRpcParser()
{
JsonRpcParser jsonRpcParser = new JsonRpcParser();
JsonRpcParserConfig config = new JsonRpcParserConfig();
config.ProtocolType = JsonRpcProtocolType.Http;//使用Tcp协议,调用时,有且仅有调用消息末尾追加“\r\n”。否则会调用失败。
config.ListenIPHosts = new RRQMSocket.IPHost[] { new RRQMSocket.IPHost(7706) };
config.ProxyToken = "RPC";//生成代理时需要验证
jsonRpcParser.Setup(config);
jsonRpcParser.Start();
Console.WriteLine("HTTP协议的JsonRpc已启动");
return jsonRpcParser;
}
static void Main(string[] args)
{
RPCService rpcService = new RPCService();
rpcService.AddRPCParser("tcpJsonRpcParser ", CreateTcpJsonRpcParser());
rpcService.AddRPCParser("httpJsonRpcParser ", CreateHTTPJsonRpcParser());
rpcService.RegisterServer<Server>();//注册服务
Console.WriteLine("RPC服务已启动");
Console.ReadKey();
}
服务在启动以后,即可调用。具体的数据格式请参照JSON-RPC 2.0 Specification。
下面将进行简单示例调用。
在服务启动后,启动网络调试助手连接,然后按图中配置,使用以下字符串测试调用。
注意: 在输入框中输入调用字符串后,需要按住Shift键+回车键,相当于在输入框中添加“\r\n”字符。
{"jsonrpc":"2.0","method":"TestJsonRpc","params":[5],"id":1}
调用时,可以使用GET、Post。Body类型也无所谓固定。但是Body的内容必须是满足JsonRpc 2.0的数据标准。
{"jsonrpc":"2.0","method":"TestJsonRpc","params":[5],"id":1}
JsonRpc在C#的语言下,RRQM提供了快捷调用客户端供大家使用。详细过程如下:
【TCP协议调用】
static void TestTcpJsonRpcParser()
{
JsonRpcClient jsonRpcClient = new JsonRpcClient();
var config = new JsonRpcClientConfig();
config.ProtocolType = JsonRpcProtocolType.Tcp;
config.RemoteIPHost = new RRQMSocket.IPHost("127.0.0.1:7705");
jsonRpcClient.Setup(config);
jsonRpcClient.Connect();
Console.WriteLine("连接成功");
while (true)
{
string result = jsonRpcClient.Invoke<string>("TestJsonRpc", InvokeOption.WaitInvoke, Console.ReadLine());
Console.WriteLine($"返回结果:{result}");
}
}
【HTTP协议调用】
static void TestHttpJsonRpcParser()
{
JsonRpcClient jsonRpcClient = new JsonRpcClient();
var config = new JsonRpcClientConfig();
config.ProtocolType = JsonRpcProtocolType.Http;
config.RemoteIPHost = new RRQMSocket.IPHost("127.0.0.1:7706");
jsonRpcClient.Setup(config);
jsonRpcClient.Connect();
Console.WriteLine("连接成功");
while (true)
{
string result = jsonRpcClient.Invoke<string>("TestJsonRpc", InvokeOption.WaitInvoke, Console.ReadLine());
Console.WriteLine($"返回结果:{result}");
}
}
上述调用,已经基本上可以满足调用了,但是当方法太多,且参数如果不是基础类型时,C#调用会变得异常麻烦。这时候,生成代理文件,然后调用,就变得很重要了。
获取代理文件详情
此时,代理中会生成Server
类,和TestJsonRpc
的接口方法。
static void TestProxy()
{
JsonRpcClient jsonRpcClient = new JsonRpcClient();
var config = new JsonRpcClientConfig();
config.ProtocolType = JsonRpcProtocolType.Http;
config.RemoteIPHost = new RRQMSocket.IPHost("127.0.0.1:7706");
jsonRpcClient.Setup(config);
jsonRpcClient.Connect();
Console.WriteLine("连接成功");
RRQMProxy.Server server = new RRQMProxy.Server(jsonRpcClient);//载入连接器
while (true)
{
string result = server.TestJsonRpc(Console.ReadLine());
Console.WriteLine($"返回结果:{result}");
}
}