更新:示例Demo

This commit is contained in:
若汝棋茗
2024-11-12 21:39:32 +08:00
parent aefe08f349
commit 8f4ed14f00
3 changed files with 109 additions and 96 deletions

View File

@@ -5,6 +5,10 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="JsonRpcProxy.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TouchSocket.JsonRpc" Version="2.1.10" />
</ItemGroup>

View File

@@ -13,6 +13,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.JsonRpc;
@@ -29,7 +30,7 @@ namespace JsonRpcConsoleApp
//4.客户端处理服务端推送的自定义消息处理
//5.[JsonRpc(true)]特性使用 标记为true 表示直接使用方法名称,否则使用命名空间+类名+方法名 全小写
//6.RPC上下文获取。通过上下文进行自定义消息推送
private static void Main(string[] args)
private static async Task Main(string[] args)
{
//{"jsonrpc": "2.0", "method": "testjsonrpc", "params":"TouchSocket", "id": 1}
@@ -39,90 +40,90 @@ namespace JsonRpcConsoleApp
ConsoleLogger.Default.Info("代理文件已经写入到当前项目。");
CreateTcpJsonRpcService();
CreateHttpJsonRpcService();
CreateWebSocketJsonRpcService();
await CreateTcpJsonRpcService();
await CreateHttpJsonRpcService();
await CreateWebSocketJsonRpcService();
Console.ReadKey();
}
private static void CreateHttpJsonRpcService()
private static async Task CreateHttpJsonRpcService()
{
var service = new HttpService();
service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7706)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<JsonRpcServer>();
});
})
.ConfigurePlugins(a =>
{
a.UseHttpJsonRpc()
.SetJsonRpcUrl("/jsonRpc");
}));
service.StartAsync();
await service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7706)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<JsonRpcServer>();
});
})
.ConfigurePlugins(a =>
{
a.UseHttpJsonRpc()
.SetJsonRpcUrl("/jsonRpc");
}));
await service.StartAsync();
ConsoleLogger.Default.Info($"Http服务器已启动");
}
private static void CreateWebSocketJsonRpcService()
private static async Task CreateWebSocketJsonRpcService()
{
var service = new HttpService();
service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7707)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<JsonRpcServer>();
});
})
.ConfigurePlugins(a =>
{
a.UseWebSocket()
.SetWSUrl("/ws");
await service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7707)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<JsonRpcServer>();
});
})
.ConfigurePlugins(a =>
{
a.UseWebSocket()
.SetWSUrl("/ws");
a.UseWebSocketJsonRpc()
.SetAllowJsonRpc((socketClient, context) =>
{
//此处的作用是通过连接的一些信息判断该ws是否执行JsonRpc。
//当然除了此处可以设置外也可以通过socketClient.SetJsonRpc(true)直接设置。
return true;
});
}));
service.StartAsync();
a.UseWebSocketJsonRpc()
.SetAllowJsonRpc((socketClient, context) =>
{
//此处的作用是通过连接的一些信息判断该ws是否执行JsonRpc。
//当然除了此处可以设置外也可以通过socketClient.SetJsonRpc(true)直接设置。
return true;
});
}));
await service.StartAsync();
ConsoleLogger.Default.Info($"WebSocket服务器已启动");
}
private static void CreateTcpJsonRpcService()
private static async Task CreateTcpJsonRpcService()
{
var service = new TcpService();
service.SetupAsync(new TouchSocketConfig()
.SetTcpDataHandlingAdapter(() => new TerminatorPackageAdapter("\r\n"))
.SetListenIPHosts(7705)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<JsonRpcServer>();
});
})
.ConfigurePlugins(a =>
{
/*
使用tcp服务器的时候默认情况下会把所有连接的协议都转换为JsonRpcUtility.TcpJsonRpc。
这样所有的数据都会被尝试解释为JsonRpc。
如果不需要该功能可以调用NoSwitchProtocol()。
*/
a.UseTcpJsonRpc();
}));
service.StartAsync();
await service.SetupAsync(new TouchSocketConfig()
.SetTcpDataHandlingAdapter(() => new TerminatorPackageAdapter("\r\n"))
.SetListenIPHosts(7705)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<JsonRpcServer>();
});
})
.ConfigurePlugins(a =>
{
/*
使用tcp服务器的时候默认情况下会把所有连接的协议都转换为JsonRpcUtility.TcpJsonRpc。
这样所有的数据都会被尝试解释为JsonRpc。
如果不需要该功能可以调用NoSwitchProtocol()。
*/
a.UseTcpJsonRpc();
}));
await service.StartAsync();
}
}
@@ -185,4 +186,6 @@ namespace JsonRpcConsoleApp
return $"a={a},b={b},c={c}";
}
}
}

View File

@@ -21,53 +21,59 @@ namespace ReverseJsonRpcConsoleApp
{
internal class Program
{
private static void Main(string[] args)
private static async Task Main(string[] args)
{
var service = GetService();
var client = GetClient();
var service = await GetService();
var client = await GetClient();
Console.ReadKey();
}
private static WebSocketJsonRpcClient GetClient()
private static async Task<WebSocketJsonRpcClient> GetClient()
{
var jsonRpcClient = new WebSocketJsonRpcClient();
jsonRpcClient.SetupAsync(new TouchSocketConfig()
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<ReverseJsonRpcServer>();
});
})
.SetRemoteIPHost("ws://127.0.0.1:7707/ws"));//此url就是能连接到websocket的路径。
jsonRpcClient.ConnectAsync();
await jsonRpcClient.SetupAsync(new TouchSocketConfig()
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<ReverseJsonRpcServer>();
});
})
.SetRemoteIPHost("ws://127.0.0.1:7707/ws"));//此url就是能连接到websocket的路径。
await jsonRpcClient.ConnectAsync();
return jsonRpcClient;
}
private static HttpService GetService()
private static async Task<HttpService> GetService()
{
var service = new HttpService();
service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7707)
.ConfigurePlugins(a =>
{
a.UseWebSocket()
.SetWSUrl("/ws");
await service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7707)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
});
})
.ConfigurePlugins(a =>
{
a.UseWebSocket()
.SetWSUrl("/ws");
a.UseWebSocketJsonRpc()
.SetAllowJsonRpc((socketClient, context) =>
{
//此处的作用是通过连接的一些信息判断该ws是否执行JsonRpc。
//当然除了此处可以设置外也可以通过socketClient.SetJsonRpc(true)直接设置。
return true;
});
a.UseWebSocketJsonRpc()
.SetAllowJsonRpc((socketClient, context) =>
{
//此处的作用是通过连接的一些信息判断该ws是否执行JsonRpc。
//当然除了此处可以设置外也可以通过socketClient.SetJsonRpc(true)直接设置。
return true;
});
a.Add<MyPluginClass>();
}));
service.StartAsync();
a.Add<MyPluginClass>();
}));
await service.StartAsync();
return service;
}
}
@@ -82,7 +88,7 @@ namespace ReverseJsonRpcConsoleApp
var jsonRpcClient = ((IHttpSessionClient)client.Client).GetJsonRpcActionClient();
var result = await jsonRpcClient.InvokeTAsync<int>("Add", InvokeOption.WaitInvoke, 10, 20);
Console.WriteLine(result);
Console.WriteLine($"反向调用成功,结果={result}");
//Stopwatch stopwatch = Stopwatch.StartNew();
//for (int i = 0; i < 10000; i++)