supersocket+controller+action

 1     public class MasterServer : SuperSocket.SocketBase.AppServer<MasterSession>
 2     {
 3         
 4     }
 5 
 6     public class MasterSession: SuperSocket.SocketBase.AppSession<MasterSession>
 7     {
 8         protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
 9         {
10             Send(Const.None);
11         }
12         
13         protected override void OnSessionStarted()
14         {
15             Console.WriteLine($"{this.RemoteEndPoint} connected, " +
16                               $"total={this.AppServer.SessionCount}");
17         }
18 
19         protected override void OnSessionClosed(CloseReason reason)
20         {
21             Console.WriteLine($"{this.RemoteEndPoint} disconnected, " +
22                               $"total={this.AppServer.SessionCount}");
23         }
24     }
25 
26     public class Master : CommandBase<MasterSession, StringRequestInfo>
27     {
28         public override void ExecuteCommand(MasterSession session, StringRequestInfo requestInfo)
29         {
30             if (requestInfo.Parameters.Length > 0)
31             {
32                 string action = requestInfo.Parameters[0];
33                 string content = "";
34                 if (requestInfo.Parameters.Length > 1)
35                 {
36                     content = requestInfo.Body.Trim().Substring(action.Length + 1);
37                 }
38                 if (action!=Const.ActionLogin && !session.Items.Keys.Contains(Const.User))
39                 {
40                     session.Send(Const.None);
41                     return;
42                 }
43                 switch (action)
44                 {
45                     case Const.ActionLogin: Login(session, content);
46                         break;
47                     default: session.Send(Const.None);
48                         break;
49                 }
50             }
51             else
52             {
53                 session.Send(Const.None);
54             }
55         }
56 
57         void Login(MasterSession session, string content)
58         {
59             var user = content.FromJson<DtoLogin>();
60             if (user.LoginName == "jonney" && user.Password == "1234")
61             {
62                 user.IsLogined = true;
63                 user.SessionId = Guid.NewGuid().ToString();
64                 user.Users = new List<int>();
65                 user.Users.Add(8);
66             }
67             session.Logger.Debug(user.ToJson());
68             if (user.IsLogined)
69             {
70                 session.Items.Remove(Const.User);
71                 session.Items.Add(Const.User, user);
72             }
73             session.Send(user.ToJson());
74         }
75         
76     }
 1 using System.Collections.Generic;
 2 
 3 namespace PublicLib
 4 {
 5     public class DtoBase
 6     {
 7         public int CurPage { get; set; }
 8         public int TotalPages { get; set; }
 9     }
10 
11     public class DtoLogin: DtoBase
12     {
13         public string LoginName { get; set; }
14         public string Password { get; set; }
15 
16         public bool IsLogined { get; set; }
17         public string SessionId { get; set; }
18         public List<int> Users { get; set; } 
19     }
20 
21     public class Const
22     {
23         public const string None = "None";
24         public const string User = "User";
25         public const string ControlerMaster = "Master";
26         public const string ActionLogin = "Login";
27     }
28 }

 supersocket作为服务器处理Pda的请求,并返回json结果,demo代码。

原文地址:https://www.cnblogs.com/jonney-wang/p/5722392.html