• 自定义AppServer


    TelnetSever.cs

     1     public class TelnetServer : AppServer<TelnetSession>
     2     {
     3         protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
     4         {
     5             return base.Setup(rootConfig, config);
     6         }
     7 
     8         protected override void OnStarted()
     9         {
    10             base.OnStarted();
    11         }
    12 
    13         protected override void OnStopped()
    14         {
    15             base.OnStopped();
    16         }
    17     }
    View Code

    TelnetSession.cs

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

    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             var appServer = new TelnetServer();
    10 
    11             var serverConfig = new ServerConfig
    12             {
    13                 Port = 8000 //set the listening port
    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 
    24             Console.WriteLine();
    25 
    26             //Try to start the appServer
    27             if (!appServer.Start())
    28             {
    29                 Console.WriteLine("Failed to start!");
    30                 Console.ReadKey();
    31                 return;
    32             }
    33 
    34             Console.WriteLine("press key 'q' to stop it!");
    35 
    36             while (Console.ReadKey().KeyChar != 'q')
    37             {
    38                 Console.WriteLine();
    39                 continue;
    40             }
    41 
    42             Console.WriteLine();
    43 
    44             //Stop the appServer
    45             appServer.Stop();
    46         }
    47     }
    View Code
  • 相关阅读:
    学习笔记: js插件 —— fullPage.js (页面全屏滚动)
    学习笔记: js插件 —— SuperSlide 2 (轮播图插件,PC用)
    学习笔记:jqchart
    学习笔记:Highcharts
    js: 字符集
    代码:PC HTML——图片列表
    教程:给初学的几个小例子(待补充)
    代码: html 页面小效果 (集合,待补充)
    MongoDB
    mongodb权限机制以及扩展
  • 原文地址:https://www.cnblogs.com/caoyc/p/4707594.html
Copyright © 2020-2023  润新知