更新:示例demo

This commit is contained in:
若汝棋茗
2024-11-24 10:55:17 +08:00
parent 013c61119d
commit ff44c27a60
4 changed files with 85 additions and 44 deletions

View File

@@ -50,6 +50,7 @@ namespace TcpWaitingClientWinFormsApp
this.groupBox1 = new GroupBox();
this.button1 = new Button();
this.button5 = new Button();
this.button4 = new Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
@@ -123,6 +124,7 @@ namespace TcpWaitingClientWinFormsApp
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button5);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.textBox4);
this.groupBox1.Controls.Add(this.textBox2);
@@ -149,7 +151,7 @@ namespace TcpWaitingClientWinFormsApp
//
// button5
//
this.button5.Location = new Point(1112, 35);
this.button5.Location = new Point(675, 36);
this.button5.Name = "button5";
this.button5.Size = new Size(150, 46);
this.button5.TabIndex = 9;
@@ -157,12 +159,22 @@ namespace TcpWaitingClientWinFormsApp
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += this.button5_Click;
//
// button4
//
this.button4.Location = new Point(872, 27);
this.button4.Name = "button4";
this.button4.Size = new Size(150, 46);
this.button4.TabIndex = 10;
this.button4.Text = "关闭服务器";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += this.button4_Click;
//
// Form1
//
this.AutoScaleDimensions = new SizeF(14F, 31F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(1437, 325);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.textBox1);
@@ -188,5 +200,6 @@ namespace TcpWaitingClientWinFormsApp
private GroupBox groupBox1;
private Button button1;
private Button button5;
private Button button4;
}
}

View File

@@ -21,6 +21,35 @@ namespace TcpWaitingClientWinFormsApp
public Form1()
{
this.InitializeComponent();
this.Load += this.Form1_Load;
}
TcpService tcpService;
private async void Form1_Load(object? sender, EventArgs e)
{
this.tcpService = await CreateService();
this.UpdateServiceButtonUI();
}
private static async Task<TcpService> CreateService()
{
var service = new TcpService();
await service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7789)
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
a.Add<MyPlugin1>();
}));
await service.StartAsync();
service.Logger.Info("Server started");
return service;
}
private TcpClient m_tcpClient;
@@ -141,5 +170,44 @@ namespace TcpWaitingClientWinFormsApp
{
cts?.Cancel();
}
private void button4_Click(object sender, EventArgs e)
{
this.tcpService?.Dispose();
this.tcpService = default;
UpdateServiceButtonUI();
}
private void UpdateServiceButtonUI()
{
if (this.tcpService==null)
{
this.button4.Text= "启动服务";
}
else
{
this.button4.Text = "停止服务";
}
}
}
class MyPlugin1 : PluginBase, ITcpReceivedPlugin
{
private readonly ILog m_logger;
public MyPlugin1(ILog logger)
{
this.m_logger = logger;
}
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
this.m_logger.Info($"Plugin:{e.ByteBlock.ToString()}");
if (client is ITcpSessionClient sessionClient)
{
await sessionClient.SendAsync(e.ByteBlock.Memory);
}
}
}
}

View File

@@ -21,52 +21,12 @@ namespace TcpWaitingClientWinFormsApp
/// The main entry point for the application.
/// </summary>
[STAThread]
private static async Task Main()
private static void Main()
{
await CreateService();
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
private static async Task CreateService()
{
var service = new TcpService();
await service.SetupAsync(new TouchSocketConfig()
.SetListenIPHosts(7789)
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
a.Add<MyPlugin1>();
}));
await service.StartAsync();
service.Logger.Info("Server started");
}
private class MyPlugin1 : PluginBase, ITcpReceivedPlugin
{
private readonly ILog m_logger;
public MyPlugin1(ILog logger)
{
this.m_logger = logger;
}
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
this.m_logger.Info($"Plugin:{e.ByteBlock.ToString()}");
if (client is ITcpSessionClient sessionClient)
{
await sessionClient.SendAsync(e.ByteBlock.Memory);
}
}
}
}
}

View File

@@ -41,7 +41,7 @@ namespace UdpDemoApp
{
this.m_udpSession.Logger.Info($"收到:{e.ByteBlock.Span.ToString(Encoding.UTF8)}");
}
var endPoint = e.EndPoint;
return EasyTask.CompletedTask;
};