会话的话 就是AppSession了 我们要开始对他进行处理了
创建一个MyAppSession类
using SuperSocket.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SuperSocketVersionTwo
{
public class MyAppSession:AppSession
{
protected override ValueTask OnSessionConnectedAsync()
{
Console.WriteLine(this.RemoteEndPoint+"已连接");
return base.OnSessionConnectedAsync();
}
protected override ValueTask OnSessionClosedAsync(EventArgs e)
{
Console.WriteLine(this.RemoteEndPoint + "已断开");
return base.OnSessionClosedAsync(e);
}
}
}
然后使用它
运行
连接断开的时候就会出现我们要打印的消息了
主程序代码
using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.ProtoBase;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.Command;
using System.Buffers;
using System;
using SuperSocket.Channel;
using System.Net;
using SuperSocket.Server;
namespace SuperSocketVersionTwo
{
class Program
{
static async Task Main(string[] args)
{
var host = SuperSocketHostBuilder.Create<StringPackageInfo, CommandLinePipelineFilter>().//创建宿主
UseCommand((o)=> {
// 一个一个的注册命令
o.AddCommand<LOGIN>();
//o.AddCommand<MULT>();
// 注册程序集重的所有命令
//o.AddCommandAssembly(typeof(SUB).GetTypeInfo().Assembly);
//添加全局命令过滤器
o.AddGlobalCommandFilter<AsyncHelloCommandFilterAttribute>();
}).UseSession<MyAppSession>().Build();
await host.RunAsync();
}
/// <summary>
/// 登录命令
/// </summary>
public class LOGIN : IAsyncCommand<StringPackageInfo>
{
public async ValueTask ExecuteAsync(IAppSession session, StringPackageInfo package)
{
System.Console.WriteLine(package.Body);
await session.SendAsync(Encoding.UTF8.GetBytes("LoginSucess" + "\r\n"));
}
}
/// <summary>
/// 全局过滤器
/// </summary>
public class AsyncHelloCommandFilterAttribute : AsyncCommandFilterAttribute
{
public override async ValueTask OnCommandExecutedAsync(CommandExecutingContext commandContext)
{
System.Console.WriteLine("Hello");
await Task.Delay(0);
}
public override async ValueTask<bool> OnCommandExecutingAsync(CommandExecutingContext commandContext)
{
System.Console.WriteLine("Bye bye");
await Task.Delay(0);
return true;
}
}
}
}
这里好多异步都用到了ValueTask
有兴趣可以去了解 好像在某些方面比较给力 能提升性能
但是上面的程序调用了父类的
base.OnSessionClosedAsync(e)
所以会出现父类又一次的提示 这个很烦
但是不知道父类是怎么处理的
为此 我去下载了SuperSocket的源码
这个问题似乎无法解决 因为他们已经写死了
改不了 嘎嘎嘎 就这样吧
————————————————
版权声明:本文为CSDN博主「亮大大大」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38083655/article/details/111633854
https://blog.csdn.net/weixin_38083655/article/details/111633854