前言
在上两篇文章中,我已经把解析器和SuperSocket初始化程序设置好了,所以下面对于接收的消息如何进行功能模块分流,进行模块小功能分类的消息处理,下面我更多的是展示代码,进行简单叙述。
对于连接事件、断开事件、接收消息事件定义与配置声明处不清楚的可以在
文章SuperSocket解析1 (初始化) 中进行回顾
主题
无脑创建以下类,包括SuperSocket 解析2 (自定义协议)中的BuiltInPipelineFilter 类和SocketCodeAnalysis类就可以跑程序了
SuperSocketCenter类
代码:
using SuperSocket.ProtoBase;
using SuperSocket;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using SuperSocket.Channel;
namespace Super
{
public class SuperSocketCenter : ISuperSocketCenter
{
private readonly ISuperSocketHostBuilder<TextPackageInfo> host = SuperSocketHostBuilder.Create<TextPackageInfo, BuiltInPipelineFilter>();
private readonly ConcurrentDictionary<string, IAppSession> keyValues = new ConcurrentDictionary<string, IAppSession>();
//提供给私发
private ConcurrentDictionary<string, string> IDCard = new ConcurrentDictionary<string, string>();
public SuperSocketCenter()
{
//this.RunSoket();
}
public void RunSoket()
{
List<ListenOptions> listens = new List<ListenOptions>();
listens.Add(new ListenOptions//配置获取
{
Ip = "any",
Port = 4040
});
host.UseSessionHandler(OnConnectedAsync, OnClosedAsync)
.UsePackageHandler(OnPackageAsync)
.ConfigureSuperSocket(options =>
{
options.Name = "PeaceTalk Server";
options.Listeners = listens;
options.DefaultTextEncoding = Encoding.UTF8;
});
host.RunConsoleAsync();
}
/// <summary>
/// 会话断开事件
/// </summary>
public async ValueTask OnClosedAsync(IAppSession session, CloseEventArgs args)
{
await Task.Run(() =>
{
while (keyValues.ContainsKey(session.SessionID))
{
if (IDCard.TryRemove(session.SessionID, out _))
{
Console.WriteLine("删除客户身份");
}
if (keyValues.TryRemove(session.SessionID, out _))
{
Console.WriteLine("删除客户ID:" + session.SessionID);
}
}
});
}
/// <summary>
///