SuperSocket 解析3 (连接事件、断开事件、消息功能)

前言

在上两篇文章中,我已经把解析器和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>
        /// 
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值