首页 > 代码库 > SuperSocket服务器架设(三):在SuperSocket中自定义Command、AppServer和AppSession
SuperSocket服务器架设(三):在SuperSocket中自定义Command、AppServer和AppSession
1. 创建自定义类MySession,继承AppSession类并重写AppSession类的方法
注:一个AppSession对象对应一个连接
[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using SuperSocket;
- using SuperSocket.Common;
- using SuperSocket.SocketBase;
- using SuperSocket.SocketBase.Protocol;
- namespace SuperSocketApp1
- {
- /// <summary>
- /// 自定义连接类MySession,继承AppSession,并传入到AppSession
- /// </summary>
- public class MySession : AppSession<MySession>
- {
- /// <summary>
- /// 新连接
- /// </summary>
- protected override void OnSessionStarted()
- {
[csharp] view plaincopyprint?
- <span style="white-space:pre"> </span> //输出客户端IP地址
- Console.WriteLine(this.LocalEndPoint.Address.ToString());
- this.Send("\n\rHello User");
- }
- /// <summary>
- /// 未知的Command
- /// </summary>
- /// <param name="requestInfo"></param>
- protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
- {
- this.Send("\n\r未知的命令");
- }
- /// <summary>
- /// 捕捉异常并输出
- /// </summary>
- /// <param name="e"></param>
- protected override void HandleException(Exception e)
- {
- this.Send("\n\r异常: {0}", e.Message);
- }
- /// <summary>
- /// 连接关闭
- /// </summary>
- /// <param name="reason"></param>
- protected override void OnSessionClosed(CloseReason reason)
- {
- base.OnSessionClosed(reason);
- }
- }
- }
2. 创建自定义类MyServer,继承AppServer类并重写AppServer类的方法
[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using SuperSocket.SocketBase;
- using SuperSocket.SocketBase.Config;
- namespace SuperSocketApp1
- {
- /// <summary>
- /// 自定义服务器类MyServer,继承AppServer,并传入自定义连接类MySession
- /// </summary>
- public class MyServer : AppServer<MySession>
- {
- protected override void OnStartup()
- {
- base.OnStartup();
- Console.WriteLine("服务器已启动");
- }
- /// <summary>
- /// 输出新连接信息
- /// </summary>
- /// <param name="session"></param>
- protected override void OnNewSessionConnected(MySession session)
- {
- base.OnNewSessionConnected(session);
[csharp] view plaincopyprint?
- <span style="white-space:pre"> </span> //输出客户端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);
- <span style="font-family: monospace; white-space: pre;"> </span><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> //输出客户端IP地址</span>
- Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":断开连接");
- }
- protected override void OnStopped()
- {
- base.OnStopped();
- Console.WriteLine("服务器已停止");
- }
- }
- }
3. 创建自定义命令HELLO类,继承CommandBase并重写ExecuteCommand方法
[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using SuperSocket.SocketBase;
- using SuperSocket.SocketBase.Command;
- using SuperSocket.SocketBase.Protocol;
- namespace SuperSocketApp1
- {
- /// <summary>
- /// 自定义命令类HELLO,继承CommandBase,并传入自定义连接类MySession
- /// </summary>
- 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("Hello World!");
- }
- }
- }
4. 修改Main方法代码
[csharp] view plaincopyprint?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using SuperSocket;
- using SuperSocket.Common;
- using SuperSocket.SocketBase;
- using SuperSocket.SocketEngine;
- using SuperSocket.SocketBase.Protocol;
- namespace SuperSocketApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var appServer = new MyServer();
- //服务器端口
- int port = 2000;
- //设置服务监听端口
- if (!appServer.Setup(port))
- {
- Console.WriteLine("端口设置失败!");
- Console.ReadKey();
- return;
- }
- //启动服务
- if (!appServer.Start())
- {
- Console.WriteLine("启动服务失败!");
- Console.ReadKey();
- return;
- }
- Console.WriteLine("启动服务成功,输入exit退出!");
- while (true)
- {
- var str = Console.ReadLine();
- if (str.ToLower().Equals("exit"))
- {
- break;
- }
- }
- Console.WriteLine();
- //停止服务
- appServer.Stop();
- Console.WriteLine("服务已停止,按任意键退出!");
- Console.ReadKey();
- }
- }
- }
5. 程序结构如图
6. 创建多服务器提供不同服务的方法:按照上面的过程,创建不同的AppServer 、AppSession子类、自定义命令,并在程序入口中启动。
7. 注意:
a) MyServer、自定义命令和MySession的访问权限必须设置为public
b) MyServer父类为AppServer<MySession>
c) MySession父类为AppSession<MySession>
d) HELLO父类为CommandBase<MySession,StringRequestInfo>,ExecueteCommand方法传入值类型分别为MySession和StringRequestInfo
e) 多服务器中需注意AppSession、AppServer、自定义命令中的AppSession不要搞错
//输出客户端IP地址
SuperSocket服务器架设(三):在SuperSocket中自定义Command、AppServer和AppSession
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。