zoukankan      html  css  js  c++  java
  • 自定义AppSession

    TelnetSession.cs

     1     public  class TelnetSession:AppSession<TelnetSession>
     2     {
     3         protected override void OnSessionStarted()
     4         {
     5             Send("Welcome to SuperSocket Telnet Server");
     6         }
     7 
     8         protected override void HandleUnknownRequest(SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
     9         {
    10             Send("Unknow request");
    11         }
    12 
    13         protected override void HandleException(Exception e)
    14         {
    15             this.Send("Application error: {0}", e.Message);
    16         }
    17 
    18         protected override void OnSessionClosed(CloseReason reason)
    19         {
    20             //add you logics which will be executed after the session is closed
    21             base.OnSessionClosed(reason);
    22         }
    23     }
    View Code

    Echo.cs

    1     public class Echo : CommandBase<TelnetSession, StringRequestInfo>
    2     {
    3         public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo)
    4         {
    5             session.Send(requestInfo.Body);
    6         }
    7     }
    View Code

    在这个command代码中,你可以看到类ECHO的父类是CommandBase<AppSession, StringRequestInfo>, 它有一个泛型参数AppSession。 是的,它是默认的AppSession类。 如果你在你的系统中使用你自己建立的AppSession类,那么你必须将你自己定义的AppSession类传进去,否则你的服务器无法发现这个Command:

    Add.cs

    1     public class Add : CommandBase<TelnetSession, StringRequestInfo>
    2     {
    3         public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo)
    4         {
    5             session.Send(requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString());
    6         }
    7     }
    View Code

    Program.cs

     1     class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Console.WriteLine("Press any key to start the server!");
     6 
     7             Console.ReadKey();
     8             Console.WriteLine();
     9 
    10             var appServer = new AppServer<TelnetSession>();
    11             var serverConfig = new ServerConfig
    12             {
    13                 Port = 8000
    14             };
    15 
    16             //Setup the appServer
    17             if (!appServer.Setup(serverConfig))
    18             {
    19                 Console.WriteLine("Failed to setup!");
    20                 Console.ReadKey();
    21                 return;
    22             }
    23             Console.WriteLine();
    24             //Try to start the appServer
    25             if (!appServer.Start())
    26             {
    27                 Console.WriteLine("Failed to start!");
    28                 Console.ReadKey();
    29                 return;
    30             }
    31 
    32             Console.WriteLine("The server started successfully, press key 'q' to stop it!");
    33 
    34             while (Console.ReadKey().KeyChar != 'q')
    35             {
    36                 Console.WriteLine();
    37                 continue;
    38             }
    39 
    40             Console.WriteLine();
    41             appServer.Stop();
    42 
    43             Console.WriteLine("The server was stopped!");
    44         }
    45     }
    View Code
  • 相关阅读:
    设计模式---003代理模式(转载自我的老师 Alley-巷子)
    设计模式---002适配模式(转载自我的老师 Alley-巷子)
    设计模式---001单例模式(转载自我的老师)
    【JavaScript算法】---希尔排序(转载自我的老师 Alley-巷子)
    【JavaScript算法】---快速排序法(转载自我的老师 Alley-巷子)
    将博客搬至CSDN
    程序员10元盒饭完整版
    querylist采集数据 模拟滑动验证码 jcapche
    php压缩图片工具类(亲测可用)
    elastic-php should匹配设置至少匹配一个条件
  • 原文地址:https://www.cnblogs.com/caoyc/p/4707516.html
Copyright © 2011-2022 走看看