supersocket+controller+action

时间:2023-12-20 17:55:26
     public class MasterServer : SuperSocket.SocketBase.AppServer<MasterSession>
{ } public class MasterSession: SuperSocket.SocketBase.AppSession<MasterSession>
{
protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
{
Send(Const.None);
} protected override void OnSessionStarted()
{
Console.WriteLine($"{this.RemoteEndPoint} connected, " +
$"total={this.AppServer.SessionCount}");
} protected override void OnSessionClosed(CloseReason reason)
{
Console.WriteLine($"{this.RemoteEndPoint} disconnected, " +
$"total={this.AppServer.SessionCount}");
}
} public class Master : CommandBase<MasterSession, StringRequestInfo>
{
public override void ExecuteCommand(MasterSession session, StringRequestInfo requestInfo)
{
if (requestInfo.Parameters.Length > )
{
string action = requestInfo.Parameters[];
string content = "";
if (requestInfo.Parameters.Length > )
{
content = requestInfo.Body.Trim().Substring(action.Length + );
}
if (action!=Const.ActionLogin && !session.Items.Keys.Contains(Const.User))
{
session.Send(Const.None);
return;
}
switch (action)
{
case Const.ActionLogin: Login(session, content);
break;
default: session.Send(Const.None);
break;
}
}
else
{
session.Send(Const.None);
}
} void Login(MasterSession session, string content)
{
var user = content.FromJson<DtoLogin>();
if (user.LoginName == "jonney" && user.Password == "")
{
user.IsLogined = true;
user.SessionId = Guid.NewGuid().ToString();
user.Users = new List<int>();
user.Users.Add();
}
session.Logger.Debug(user.ToJson());
if (user.IsLogined)
{
session.Items.Remove(Const.User);
session.Items.Add(Const.User, user);
}
session.Send(user.ToJson());
} }
 using System.Collections.Generic;

 namespace PublicLib
{
public class DtoBase
{
public int CurPage { get; set; }
public int TotalPages { get; set; }
} public class DtoLogin: DtoBase
{
public string LoginName { get; set; }
public string Password { get; set; } public bool IsLogined { get; set; }
public string SessionId { get; set; }
public List<int> Users { get; set; }
} public class Const
{
public const string None = "None";
public const string User = "User";
public const string ControlerMaster = "Master";
public const string ActionLogin = "Login";
}
}

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