C# SuperSocket 基础六【CountSpliterReceiveFilte-固定数量分隔符协议】使用COMMAND
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