SuperSocket服务器架设(四):我的AppSession连接管理方案(简单版)

1.        AppSession连接管理的用处:

a)        服务器向客户端批量推送信息

b)       客户端对客户端的通信

 

2.        创建客户端管理类MySessionManager,添加静态列表SessionList

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Collections;  
  6.   
  7. namespace SuperSocketApp1  
  8. {  
  9.     /// <summary>  
  10.     /// MySession对应连接储存类  
  11.     /// </summary>  
  12.     public static class MySessionManager  
  13.     {  
  14.         public static List<MySession> SessionList = new List<MySession>();  
  15.     }  
  16. }  

3.        SessionList的添加与移除方法1:

a)        在MySession的OnSessionStarted()方法中添加如下代码

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. MySessionManager.SessionList.Add(this);  

 

b)       在MySession的OnSessionClosed()方法中添加如下代码

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. MySessionManager.SessionList.Remove(this);  

4.        SessionList的添加与移除方法2:

a)        在MyServer的OnNewSessionConnected()方法中添加如下代码

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. MySessionManager.SessionList.Add(session);  

b)       在MyServer的OnSessionClosed()方法中添加如下代码

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. MySessionManager.SessionList.Remove(session);  

5.        向客户端推送消息的代码可以自由发挥,比如以自定义命令的方式实现:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket.SocketBase;  
  6. using SuperSocket.SocketBase.Command;  
  7. using SuperSocket.SocketBase.Protocol;  
  8.   
  9. namespace SuperSocketApp1  
  10. {  
  11.     /// <summary>  
  12.     /// 向客户端推送信息  
  13.     /// </summary>  
  14.     public class PUSH : CommandBase<MySession, StringRequestInfo>  
  15.     {  
  16.         public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)  
  17.         {  
  18.             /*  
  19.              * 内置命令行协议: 
  20.              *      命令行协议定义了每个请求必须以回车换行结尾 "\r\n"。 
  21.              *      用空格来分割请求的Key和参数 
  22.              *      requestInfo.Body 参数 
  23.              *      requestInfo.Parameters 用空格分隔参数,返回数组 
  24.              */  
  25.             foreach (MySession tmp in MySessionManager.SessionList)  
  26.             {  
  27.                 tmp.Send(requestInfo.Body);  
  28.             }  
  29.         }  
  30.     }  
  31. }  

6.        客户端间的通信过程为:获取客户端列表,选择客户端并发送消息(这里通过自定义命令实现)

a)        获取客户端列表:创建LIST命令,代码如下:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket.SocketBase;  
  6. using SuperSocket.SocketBase.Command;  
  7. using SuperSocket.SocketBase.Protocol;  
  8.   
  9. namespace SuperSocketApp1  
  10. {  
  11.     public class LIST : CommandBase<MySession, StringRequestInfo>  
  12.     {  
  13.         /// <summary>  
  14.         /// 获取客户端列表  
  15.         /// </summary>  
  16.         /// <param name="session"></param>  
  17.         /// <param name="requestInfo"></param>  
  18.         public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)  
  19.         {  
  20.             var tmp = "";  
  21.             //判断clentList中是否有值  
  22.             if (MySessionManager.SessionList.Count < 1)  
  23.             {  
  24.                 tmp = "NoBody.";  
  25.             }  
  26.             else  
  27.             {  
  28.                 for (int i = 0; i < MySessionManager.SessionList.Count; i++)  
  29.                 {  
  30.                     //SessionID为Session的唯一标示  
  31.                     tmp += "Id:" + (i + 1) + ",SessionID:" + MySessionManager.SessionList[i].SessionID;  
  32.                 }  
  33.             }  
  34.             session.Send(tmp);  
  35.         }  
  36.     }  
  37. }  


b)       选择客户端并发送消息:创建SEND命令,代码如下:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket.SocketBase;  
  6. using SuperSocket.SocketBase.Command;  
  7. using SuperSocket.SocketBase.Protocol;  
  8.   
  9. namespace SuperSocketApp1  
  10. {  
  11.     /// <summary>  
  12.     /// 向客户端推送信息  
  13.     /// </summary>  
  14.     public class SEND : CommandBase<MySession, StringRequestInfo>  
  15.     {  
  16.         public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)  
  17.         {  
  18.             /*  
  19.              * 内置命令行协议: 
  20.              *      命令行协议定义了每个请求必须以回车换行结尾 "\r\n"。 
  21.              *      用空格来分割请求的Key和参数 
  22.              *      requestInfo.Body 参数 
  23.              *      requestInfo.Parameters 用空格分隔参数,返回数组 
  24.              */  
  25.             //通过参数的个数判断命令类型  
  26.             if (requestInfo.Parameters.Length < 2)  
  27.             {  
  28.                 session.Send("命令格式:send id 信息");  
  29.                 //send 信息  
  30.                 //for (int i = 0; i < Client.clientList.Count; i++)  
  31.                 //{  
  32.                 //    //取SessionId对应的Session并发送信息  
  33.                 //    Client.TestServer.GetAppSessionByID(Client.clientList[i]).Send(requestInfo.Body);  
  34.                 //}  
  35.             }  
  36.             else  
  37.             {  
  38.                 //send id 信息  
  39.                 //判断id是否合法  
  40.                 if (int.Parse(requestInfo.Parameters[0]) < 1 || int.Parse(requestInfo.Parameters[0]) > MySessionManager.SessionList.Count)  
  41.                 {  
  42.                     session.Send("id错误。");  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                     int i = int.Parse(requestInfo.Parameters[0]);  
  47.                     //取Session并发送信息  
  48.                     MySessionManager.SessionList[i - 1].Send(requestInfo.Parameters[1]);  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53. }  

7.        目录结构:

 

 

8.        MyServer和MySession最终代码:

  a)        MyServer代码:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket.SocketBase;  
  6. using SuperSocket.SocketBase.Config;  
  7.   
  8. namespace SuperSocketApp1  
  9. {  
  10.     /// <summary>  
  11.     /// 自定义服务器类MyServer,继承AppServer,并传入自定义连接类MySession  
  12.     /// </summary>  
  13.     public class MyServer : AppServer<MySession>  
  14.     {  
  15.         protected override void OnStartup()  
  16.         {  
  17.             base.OnStartup();  
  18.             Console.WriteLine("服务器已启动");  
  19.         }  
  20.   
  21.         /// <summary>  
  22.         /// 输出新连接信息  
  23.         /// </summary>  
  24.         /// <param name="session"></param>  
  25.         protected override void OnNewSessionConnected(MySession session)  
  26.         {  
  27.             base.OnNewSessionConnected(session);  
  28.             //客户端建立连接,将session加入list  
  29.             MySessionManager.SessionList.Add(session);  
  30.             Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":连接");  
  31.         }  
  32.   
  33.         /// <summary>  
  34.         /// 输出断开连接信息  
  35.         /// </summary>  
  36.         /// <param name="session"></param>  
  37.         /// <param name="reason"></param>  
  38.         protected override void OnSessionClosed(MySession session, CloseReason reason)  
  39.         {  
  40.             base.OnSessionClosed(session, reason);  
  41.             //客户端断开连接,移除session  
  42.             MySessionManager.SessionList.Remove(session);  
  43.             Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":断开连接");  
  44.         }  
  45.   
  46.         protected override void OnStopped()  
  47.         {  
  48.             base.OnStopped();  
  49.             Console.WriteLine("服务器已停止");  
  50.         }  
  51.     }  
  52. }  

  b)        MySession代码:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using SuperSocket;  
  6. using SuperSocket.Common;  
  7. using SuperSocket.SocketBase;  
  8. using SuperSocket.SocketBase.Protocol;  
  9.   
  10. namespace SuperSocketApp1  
  11. {  
  12.     /// <summary>  
  13.     /// 自定义连接类MySession,继承AppSession,并传入到AppSession  
  14.     /// </summary>  
  15.     public class MySession : AppSession<MySession>  
  16.     {  
  17.         /// <summary>  
  18.         /// 新连接  
  19.         /// </summary>  
  20.         protected override void OnSessionStarted()  
  21.         {  
  22.             MySessionManager.SessionList.Add(this);  
  23.             Console.WriteLine(this.LocalEndPoint.Address.ToString());  
  24.             this.Send("\n\rHello User");  
  25.         }  
  26.   
  27.         /// <summary>  
  28.         /// 未知的Command  
  29.         /// </summary>  
  30.         /// <param name="requestInfo"></param>  
  31.         protected override void HandleUnknownRequest(StringRequestInfo requestInfo)  
  32.         {  
  33.             this.Send("\n\r未知的命令");  
  34.         }  
  35.   
  36.         /// <summary>  
  37.         /// 捕捉异常并输出  
  38.         /// </summary>  
  39.         /// <param name="e"></param>  
  40.         protected override void HandleException(Exception e)  
  41.         {  
  42.             this.Send("\n\r异常: {0}", e.Message);  
  43.         }  
  44.   
  45.         /// <summary>  
  46.         /// 连接关闭  
  47.         /// </summary>  
  48.         /// <param name="reason"></param>  
  49.         protected override void OnSessionClosed(CloseReason reason)  
  50.         {  
  51.             MySessionManager.SessionList.Remove(this);  
  52.             base.OnSessionClosed(reason);  
  53.         }  
  54.     }  
  55. }  

9.        注:AppServer类自带方法GetAllSessions()用于获取所有的AppSession:

a)        AppServer中:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. foreach (var session in this.GetAllSessions())  
  2.             {  
  3.                 session.Send(now);  
  4.             }  

  b)        其他地方:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. foreach (var session in AppServer.GetAllSessions())  
  2.             {  
  3.                 session.Send(now);  
  4.             }  


  a)        MyServer代码:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值