定义服务
在服务器端中新建一个类,继承于ServerProvider类(或实现IServerProvider),然后在该类中写公共方法,并用JsonRpc属性标签标记,如果方法有重载,需要重新指定函数键。
支持代理生成注释。
public class Server : ServerProvider{[JsonRpc]public string TestJsonRpc(string str){return "RRQM" + str;}[JsonRpc]public JObject TestJObject(JObject obj){return obj;}}
创建服务解析器
服务解析器是实际的服务接收、触发、调用、反馈的实际载体,通俗来说就是通信服务器。
【Tcp协议解析器】
当JsonRpcParserPlugin插件添加在TcpService服务器时,会使用TCP协议。此时调用端必须以\r\n结尾。
static IRpcParser CreateTcpJsonRpcParser(){TcpService service = new TcpService();service.Connecting += (client, e) =>{client.SetDataHandlingAdapter(new TerminatorPackageAdapter("\r\n"));};service.Setup(new RRQMConfig().UsePlugin().SetListenIPHosts(new IPHost[] { new IPHost(7705) })).Start();return service.AddPlugin<JsonRpcParserPlugin>();}
【Http解析器】
当JsonRpcParserPlugin插件添加在HttpService服务器时,会使用Http协议。
static IRpcParser CreateHTTPJsonRpcParser(){HttpService service = new HttpService();service.Setup(new RRQMConfig().UsePlugin().SetListenIPHosts(new IPHost[] { new IPHost(7706) })).Start();return service.AddPlugin<JsonRpcParserPlugin>().SetJsonRpcUrl("/jsonRpc");}
注册、发布服务
添加解析器(添加时需要以键、值方式添加,方便后续查找),然后注册服务即可。
static void Main(string[] args){RpcStore rpcStore = new RpcStore();//添加解析器,解析器根据传输协议,序列化方式的不同,调用RPC服务rpcStore.AddRpcParser("tcpJsonRpcParser ", CreateTcpJsonRpcParser());rpcStore.AddRpcParser("httpJsonRpcParser ", CreateHTTPJsonRpcParser());//注册当前程序集的所有服务rpcStore.RegisterAllServer();//分享代理,代理文件可通过RRQMTool远程获取。//rpcService.ShareProxy(new IPHost(8848));//或者直接本地导出代理文件。//ServerCellCode[] cellCodes = rpcStore.GetProxyInfo(typeof(JsonRpcAttribute));//当想导出全部时,RpcStore.ProxyAttributeMap.Values.ToArray()//string codeString = CodeGenerator.ConvertToCode("RRQMProxy", cellCodes);JsonRpcClientInvokeByTcp();JsonRpcClientInvokeByHttp();Console.ReadKey();}
