提交 eef56dd1 编写于 作者: 若汝棋茗

完善jsonrpc的websocket协议调用

上级 f59af66b
......@@ -9,6 +9,7 @@ using TouchSocket.Core.Config;
using TouchSocket.Core.Plugins;
using TouchSocket.Core.XREF.Newtonsoft.Json.Linq;
using TouchSocket.Http;
using TouchSocket.Http.WebSockets;
using TouchSocket.Rpc;
using TouchSocket.Rpc.JsonRpc;
using TouchSocket.Rpc.TouchRpc;
......@@ -16,83 +17,31 @@ using TouchSocket.Sockets;
namespace JsonRpcConsoleApp
{
public class JsonRpcServer : RpcServer
{
/// <summary>
/// 使用调用上下文。
/// 可以从上下文获取调用的SocketClient。从而获得IP和Port等相关信息。
/// </summary>
/// <param name="callContext"></param>
/// <param name="str"></param>
/// <returns></returns>
[JsonRpc(MethodFlags = MethodFlags.IncludeCallContext)]
public string TestGetContext(ICallContext callContext, string str)
{
if (callContext.Caller is HttpSocketClient socketClient)
{
Console.WriteLine("HTTP请求");
var client = callContext.Caller as HttpSocketClient;
var ip = client.IP;
var port = client.Port;
Console.WriteLine($"HTTP请求{ip}:{port}");
}
else if (callContext.Caller is SocketClient)
{
Console.WriteLine("Tcp请求");
var client = callContext.Caller as SocketClient;
var ip = client.IP;
var port = client.Port;
Console.WriteLine($"Tcp请求{ip}:{port}");
}
return "RRQM" + str;
}
[JsonRpc]
public JObject TestJObject(JObject obj)
{
return obj;
}
[JsonRpc]
public string TestJsonRpc(string str)
{
return "RRQM" + str;
}
/// <summary>
/// 当标记为true时直接使用方法名称
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[JsonRpc(true)]
public string TestJsonRpc1(string str)
{
return "RRQM" + str;
}
}
internal class Program
{
private static IRpcParser CreateHTTPJsonRpcParser(int port)
private static void CreateHTTPJsonRpcParser(int port)
{
HttpService service = new HttpService();
service.Setup(new TouchSocketConfig()
.UsePlugin()
.SetListenIPHosts(new IPHost[] { new IPHost(port) })
.ConfigureRpcStore(a =>
.ConfigureRpcStore(a =>//Rpc的配置必须在插件之前。
{
a.RegisterServer<JsonRpcServer>();
})
.ConfigurePlugins(a =>
{
a.Add<WebSocketServerPlugin>()
.SetWSUrl("/ws");//启用websocket,使用/ws路由连接。
a.Add<JsonRpcParserPlugin>()
.SetJsonRpcUrl("/jsonRpc");
}))
.Start();
return service.AddPlugin<JsonRpcParserPlugin>()
.SetJsonRpcUrl("/jsonRpc");
}
private static IRpcParser CreateTcpJsonRpcParser(int port)
private static void CreateTcpJsonRpcParser(int port)
{
TcpService service = new TcpService();
service.Connecting += (client, e) =>
......@@ -114,13 +63,15 @@ namespace JsonRpcConsoleApp
.UsePlugin()
.SetDataHandlingAdapter(() => new TerminatorPackageAdapter("\r\n"))//使用这个适配器,防止粘包。但是也要求json中没有换行符。
.SetListenIPHosts(new IPHost[] { new IPHost(port) })
.ConfigureRpcStore(a =>
.ConfigureRpcStore(a =>
{
a.RegisterServer<JsonRpcServer>();
}))
})
.ConfigurePlugins(a =>
{
a.Add<JsonRpcParserPlugin>();
}))
.Start();
return service.AddPlugin<JsonRpcParserPlugin>();
}
private static void JsonRpcClientInvokeByHttp()
......@@ -147,7 +98,7 @@ namespace JsonRpcConsoleApp
JsonRpcClient jsonRpcClient = new JsonRpcClient();
jsonRpcClient.Setup(new TouchSocketConfig()
.SetRemoteIPHost("127.0.0.1:7705")
.SetDataHandlingAdapter(()=> new TerminatorPackageAdapter("\r\n"))
.SetDataHandlingAdapter(() => new TerminatorPackageAdapter("\r\n"))
.SetJRPT(JRPT.Tcp));
jsonRpcClient.Connect();
......@@ -169,6 +120,33 @@ namespace JsonRpcConsoleApp
Console.WriteLine($"Tcp返回结果:{newObj}");
}
private static void JsonRpcClientInvokeByWebsocket()
{
JsonRpcClient jsonRpcClient = new JsonRpcClient();
jsonRpcClient.Setup(new TouchSocketConfig()
.SetRemoteIPHost("ws://127.0.0.1:7706/ws")//此url就是能连接到websocket的路径。
.SetDataHandlingAdapter(() => new TerminatorPackageAdapter("\r\n"))
.SetJRPT(JRPT.Websocket));
jsonRpcClient.Connect();
Console.WriteLine("连接成功");
string result = jsonRpcClient.TestJsonRpc("RRQM");
Console.WriteLine($"Websocket返回结果:{result}");
result = jsonRpcClient.TestJsonRpc1("RRQM");
Console.WriteLine($"Websocket返回结果:{result}");
result = jsonRpcClient.TestGetContext("RRQM");
Console.WriteLine($"Websocket返回结果:{result}");
JObject obj = new JObject();
obj.Add("A", "A");
obj.Add("B", 10);
obj.Add("C", 100.1);
JObject newObj = jsonRpcClient.TestJObject(obj);
Console.WriteLine($"Websocket返回结果:{newObj}");
}
//JSONRPC 通讯基础示例 TPC和HTTP 未来考虑扩展升级WebSocket
//1.完成了JSONRPC 的基本调用方法
//2.JSONRPC 服务端和客户端的创建
......@@ -190,11 +168,68 @@ namespace JsonRpcConsoleApp
JsonRpcClientInvokeByTcp();
JsonRpcClientInvokeByHttp();
JsonRpcClientInvokeByWebsocket();
Console.WriteLine("请按任意键退出");
Console.ReadKey();
}
}
public class JsonRpcServer : RpcServer
{
/// <summary>
/// 使用调用上下文。
/// 可以从上下文获取调用的SocketClient。从而获得IP和Port等相关信息。
/// </summary>
/// <param name="callContext"></param>
/// <param name="str"></param>
/// <returns></returns>
[JsonRpc(MethodFlags = MethodFlags.IncludeCallContext)]
public string TestGetContext(ICallContext callContext, string str)
{
if (callContext.Caller is HttpSocketClient socketClient)
{
Console.WriteLine("HTTP请求");
var client = callContext.Caller as HttpSocketClient;
var ip = client.IP;
var port = client.Port;
Console.WriteLine($"HTTP请求{ip}:{port}");
}
else if (callContext.Caller is SocketClient)
{
Console.WriteLine("Tcp请求");
var client = callContext.Caller as SocketClient;
var ip = client.IP;
var port = client.Port;
Console.WriteLine($"Tcp请求{ip}:{port}");
}
return "RRQM" + str;
}
[JsonRpc]
public JObject TestJObject(JObject obj)
{
return obj;
}
[JsonRpc]
public string TestJsonRpc(string str)
{
return "RRQM" + str;
}
/// <summary>
/// 当标记为true时直接使用方法名称
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[JsonRpc(true)]
public string TestJsonRpc1(string str)
{
return "RRQM" + str;
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册