博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义AppSession
阅读量:4576 次
发布时间:2019-06-08

本文共 2952 字,大约阅读时间需要 9 分钟。

 

TelnetSession.cs

1     public  class TelnetSession:AppSession
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 closed21 base.OnSessionClosed(reason);22 }23 }
View Code

 

Echo.cs

1     public class Echo : CommandBase
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
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
();11 var serverConfig = new ServerConfig12 {13 Port = 800014 };15 16 //Setup the appServer17 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 appServer25 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

 

转载于:https://www.cnblogs.com/caoyc/p/4707516.html

你可能感兴趣的文章
为什么使用Nosql:Nosql和SQL的区别
查看>>
<转>DNS服务系列之二:DNS区域传送漏洞的安全案例
查看>>
LINUX中常用操作命令
查看>>
【android】动画效果研究(View)【1】
查看>>
(三)常用的数学函数
查看>>
生产信息集成分析平台(MIIAS V1.0) 概述
查看>>
学习进度——第十五周
查看>>
简谈-网络爬虫的几种常见类型
查看>>
File对象目录列表器
查看>>
(K)ubuntu上将分区格式化成NTFS格式
查看>>
uva 12003 Array Transformer (大规模阵列)
查看>>
mysql5.7二进制包安装方式
查看>>
装饰者模式——Java设计模式
查看>>
39.递推练习: 菲波那契数列(2)
查看>>
排序精讲
查看>>
【bzoj3172】 Tjoi2013—单词
查看>>
【uoj2】 NOI2014—起床困难综合症
查看>>
js return的用法
查看>>
子节点填充父元素除去一固定高度后的剩余高度
查看>>
[原]IOS 后台发送邮件
查看>>