SuperSocket实例
下载地址:https://files.cnblogs.com/files/xixixing/ConsoleApp.zip
创建控制台应用程序:ConsoleApp
MySession.cs
using SuperSocket.SocketBase; using SuperSocket.SocketBase.Protocol; using System; namespace ConsoleApp { /// <summary> /// 用来发送和接收客户端信息,一个客户端相当于一个session(会话)。 /// 自定义连接类MySession,继承AppSession,并传入到AppSession /// </summary> public class MySession : AppSession<MySession> { /// <summary> /// 新连接 /// </summary> //protected override void OnSessionStarted() //{ // this.Send("\r\nHello User"); //} /// <summary> /// 未知的请求 /// </summary> /// <param name="requestInfo"></param> protected override void HandleUnknownRequest(StringRequestInfo requestInfo) { //this.Send("\r\nUnknown Request"); } /// <summary> /// 捕捉异常并输出 /// </summary> /// <param name="e"></param> protected override void HandleException(Exception e) { //this.Send("\r\nException: {0}", e.Message); } /// <summary> /// 连接关闭 /// </summary> /// <param name="reason"></param> protected override void OnSessionClosed(CloseReason reason) { base.OnSessionClosed(reason); } } }
MyServer.cs
using SuperSocket.SocketBase; using System; namespace ConsoleApp { /// <summary> /// 自定义服务器类MyServer,继承AppServer,并传入自定义连接类MySession /// </summary> public class MyServer : AppServer<MySession> { protected override void OnStarted() { base.OnStarted(); //Console.WriteLine("服务器已启动"); } /// <summary> /// 输出新连接信息 /// </summary> /// <param name="session"></param> protected override void OnNewSessionConnected(MySession 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(MySession session, CloseReason reason) { base.OnSessionClosed(session, reason); //输出客户端IP地址 Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":断开连接"); } protected override void OnStopped() { base.OnStopped(); Console.WriteLine("服务器已停止"); } } }
Program.cs
using System; using SuperSocket.SocketBase; using SuperSocket.SocketEngine; namespace ConsoleApp { class Program { static void Main(string[] args) { var bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) //启动SuperSocket { Console.WriteLine("初始化失败!"); Console.ReadKey(); return; } var result = bootstrap.Start(); Console.WriteLine("服务正在启动: {0}!", result); if (result == StartResult.Failed) { Console.WriteLine("服务启动失败!"); Console.ReadKey(); return; } Console.WriteLine("服务启动成功,请按'E'停止服务!"); while (Console.ReadKey().KeyChar != 'E') { Console.WriteLine(); continue; } //停止服务 bootstrap.Stop(); Console.WriteLine("服务已停止!"); Console.ReadKey(); } } }
MyKey.cs,写了两个Key:HELLO、READY,响应的结果都是1234
using SuperSocket.SocketBase.Command; using SuperSocket.SocketBase.Protocol; namespace ConsoleApp { public class HELLO : CommandBase<MySession, StringRequestInfo> { /// <summary> /// 自定义执行命令方法,注意传入的变量session类型为MySession /// </summary> /// <param name="session"></param> /// <param name="requestInfo"></param> public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo) { session.Send("1234"); } } public class READY : CommandBase<MySession, StringRequestInfo> { public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo) { session.Send("1234"); } } }
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <!--log 日志记录--> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> <!--SocketEngine--> <section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/> </configSections> <!--服务信息描述,在window服务模式下的名称标识--> <appSettings> <add key="ServiceName" value="SupperSocketService"/> <add key="ServiceDescription" value="黄昏前黎明后的Socket程序"/> </appSettings> <!--SuperSocket服务配置信息 serverType是项目的服务,如我自定义的MyServer--> <!--name: 实例名称 textEncoding 编码方式"gb2312","utf-8" 默认是acii serverType: 实例运行的AppServer类型。逗号左边的是你自定义的server在项目中的位置,逗号右边是项目名 ip: 服务器ip port: 侦听端口--> <superSocket> <servers> <server name="ConsoleApp" textEncoding="gb2312" serverType="ConsoleApp.MyServer,ConsoleApp" ip="Any" port="2000"> <!--ip="192.168.0.40"--> </server> </servers> </superSocket> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>