首页 > 编程语言 >C# SuperSocket 基础六【CountSpliterReceiveFilte-固定数量分隔符协议】使用COMMAND

C# SuperSocket 基础六【CountSpliterReceiveFilte-固定数量分隔符协议】使用COMMAND

时间:2024-10-28 15:48:42浏览次数:7  
标签:Console SuperSocket C# void session COMMAND WriteLine override base

public class CountSpliterReceiveFilterSession : AppSession<CountSpliterReceiveFilterSession>
    {
        public override void Send(string message)
        {
            Console.WriteLine("发送消息:" + message);
            base.Send(message);
        }

        protected override void OnSessionStarted()
        {
            //输出客户端IP地址  
            Console.WriteLine(this.LocalEndPoint.Address.ToString());
            this.Send("Hello User,Welcome to SuperSocket Telnet Server!");
        }


        /// <summary>  
        /// 连接关闭  
        /// </summary>  
        /// <param name="reason"></param>  
        protected override void OnSessionClosed(CloseReason reason)
        {
            base.OnSessionClosed(reason);
        }


        protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
        {
            Console.WriteLine($"遇到未知的请求 Key:" + requestInfo.Key + $" Body:" + requestInfo.Body);
            base.HandleUnknownRequest(requestInfo);
        }

        /// <summary>  
        /// 捕捉异常并输出  
        /// </summary>  
        /// <param name="e"></param>  
        protected override void HandleException(Exception e)
        {
            this.Send("error: {0}", e.Message);
        }
    }
public class CountSpliterReceiveFilterServer : AppServer<CountSpliterReceiveFilterSession>
    {
        /// 请求格式:#part1#part2#part3#part4#part5#part6#part7#
        public CountSpliterReceiveFilterServer()
           : base(new CountSpliterReceiveFilterFactory((byte)'#', 8)) //8个分隔符,7个参数。除使用默认的过滤工厂,还可以参照上一个实例定制协议
        {

        }

        protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
        {
            Console.WriteLine("正在准备配置文件");
            return base.Setup(rootConfig, config);
        }

        protected override void OnStarted()
        {
            Console.WriteLine("服务已开始");
            base.OnStarted();
        }

        protected override void OnStopped()
        {
            Console.WriteLine("服务已停止");
            base.OnStopped();
        }

        /// <summary>  
        /// 输出新连接信息  
        /// </summary>  
        /// <param name="session"></param>  
        protected override void OnNewSessionConnected(CountSpliterReceiveFilterSession session)
        {
            base.OnNewSessionConnected(session);
            //输出客户端IP地址  
            Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":连接");
        }


        /// <summary>  
        /// 输出断开连接信息  
        /// </summary>  
        /// <param name="session"></param>  
        /// <param name="reason"></param>  
        protected override void OnSessionClosed(CountSpliterReceiveFilterSession session, CloseReason reason)
        {
            base.OnSessionClosed(session, reason);
            Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":断开连接");
        }

    }
 public class LOGIN : CommandBase<CountSpliterReceiveFilterSession, StringRequestInfo>
    {
        /// <summary>  
        /// 自定义执行命令方法,注意传入的变量session类型为MySession  
        /// </summary>  
        /// <param name="session">会话</param>  
        /// <param name="requestInfo">请求数据信息</param>  
        public override void ExecuteCommand(CountSpliterReceiveFilterSession session, StringRequestInfo requestInfo)
        {
            session.Send(string.Format("LOGIN {0}:{1} {2}", session.Config.Ip, session.Config.Port, requestInfo.Body));
        }
    }
 Console.WriteLine("请按任何键进行启动SuperSocket服务!");
            //Console.ReadKey();
            //Console.WriteLine();
            var bootstrap = BootstrapFactory.CreateBootstrap();

            if (!bootstrap.Initialize())
            {
                //Console.WriteLine("初始化失败!");
                //Console.ReadKey();
                return;
            }

            var result = bootstrap.Start();

            //Console.WriteLine("服务正在启动: {0}!", result);

            if (result == StartResult.Failed)
            {
                Console.WriteLine("服务启动失败!");
                Console.ReadKey();
                return;
            }
            //Console.WriteLine("服务启动成功,请按'q'停止服务!");

            //while (Console.ReadKey().KeyChar != 'q')
            //{
            //    Console.WriteLine();
            //    continue;
            //}

            ////停止服务
            //bootstrap.Stop();
            //Console.WriteLine("服务已停止!");
            //Console.ReadKey();
来源:https://blog.csdn.net/ba_wang_mao/article/details/128283899

 

标签:Console,SuperSocket,C#,void,session,COMMAND,WriteLine,override,base
From: https://www.cnblogs.com/ywtssydm/p/18510572

相关文章

  • CSP游记
    Day-1下午与同学一同去试机,结果ST与家里装的插件不同,只能用DEV了,接着看了看22年的题目,想出来了T4部分分后就走了。晚上在机房颓废,不知道干什么,就重新写了遍动态开点,挂了,调了半个小时才过。Day1上午提前20分钟进了机房,打完模板后写了个最短路板子。前面两道题都......
  • Surface Splatting
    AbstractModernlaserrangeandopticalscannersneedrenderingtechniquesthatcanhandlemillionsofpointswithhighresolutiontextures.Thispaperdescribesapointrenderingandtexturefilteringtechniquecalledsurfacesplattingwhichdirectlyren......
  • A. Build a Computer
    独立做出了银牌题~线段树划分区间其实无须左闭右开点击查看代码#include<bits/stdc++.h>usingnamespacestd;inttot,id;vector<int>a[1005],c[1005];intt[1005][2];voidadd(intu,intv,intw){a[u].push_back(v);c[u].push_back(w);}voidask(intl,......
  • Gaussian Splatting with NeRF-based Color and Opacity
    AbstractNeRFs havedemonstratedtheremarkablepotentialofneuralnetworkstocapturetheintricaciesof3Dobjects.Byencodingtheshapeandcolorinformationwithinneuralnetworkweights,NeRFsexcelatproducingstrikinglysharpnovelviewsof3Do......
  • 扣子(Coze)
    一、产品描述扣子(英文名称为Coze)是由字节跳动推出的新一代一站式AIBot开发平台。它于2024年2月1日正式上线,旨在让非开发者也能快速创建、调试和优化聊天机器人。扣子平台基于大模型技术,客户无需具备深厚的编程基础,即可在平台上轻松搭建和发布各类智能Bot。体验地址:扣子(Coze)......
  • 基于Pyecharts的数据可视化开发(二)调用通义千问api分析爬虫数据
            上一篇博客做了关于“广州市2023年天气情况”的数据爬取,并保存为.csv文件。下一步是想用生成的.csv文件,直接调用大模型api进行分析,得出结论。通过调研,阿里云的通义千问大模型qwen-long可以实现对文件数据的分析。        通义千问大模型提供了API,可......
  • 认证 (authentication) 和授权 (authorization) 的区别
    authenticationauthorization英/ɔːˌθentɪˈkeɪʃn/ 美/ɔːˌθentɪˈkeɪʃn/ n.证明真实性,鉴定;身份验证,认证英/ˌɔːθəraɪˈzeɪʃ(ə)n/ 美/ˌɔːθərəˈzeɪʃ(ə)n/ n.批准书,授权书;批准,授权网络授权/授......
  • react数组插入
    1、定义数组:const[items,setItems]=useState([]);2、普通js写法插入:setItems([...items,newItem])但是由于react是异步渲染的,这种更新方式可能会导致渲染不同步3、推荐更新方式:使用setState方法,并提供一个函数,该函数接收先前的状态,并返回一个更新后的状态。constaddIte......
  • Excel-多表数据查找匹配(VLOOKUP)
    ......
  • CSP-S 2024 游记
    CSP-S2024Day-2晚自习听到了Danubius的建议,有了这一篇游记。Day-1吃完晚饭后只有我一个人留在机房(其他人都请假回家了)。晚自习做了几道题。看到QQ群里没人说话,回到宿舍发现原来是都藏在微信群里了。看了下微信后就休息了。Day0早上先去机房打了两个板子,八点整的时候hxl......