什么是AppSession?
AppSession代表一个和客户端的逻辑连接,基于连接的操作应该定于在该类之中。您可以使用该类的实例发送数据到客户端,接收客户端发送的数据或关闭连接。
什么是AppServer?
理想情况下,我们可以通过AppServer实例获取任何您想要的客户端连接,服务器级别的操作和逻辑应该定义在此类之中。
了解一下
其实我们所作的大部分逻辑处理
都是针对与AppSession的
SuperSocketAppServer和AppSession
原生提供的可实现的方法很少
使用的又是dll 我们改不了它里面的代码
但是它留给我们接口 让我去自定义自己的服务
就很棒哦
我们接下来 对前面教程使用的例子继续进行修改
首先
创建这么两个文件夹
两个文件夹里面分别建立这么两个类
Server
using Socket.appSession;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Socket.appServer
{
public class MyAppServer: AppServer<MyAppSession>
{
protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
{
return base.Setup(rootConfig, config);
}
[Obsolete]
protected override void OnStartup()
{
base.OnStartup();
}
protected override void OnStopped()
{
base.OnStopped();
}
}
}
Session
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Socket.appSession
{
public class MyAppSession : AppSession<MyAppSession>
{
protected override void OnSessionStarted()
{
this.Send("Welcome to SuperSocket Telnet Server");
}
protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
{
this.Send("Unknow request");
}
protected override void HandleException(Exception e)
{
this.Send("Application error: {0}", e.Message);
}
protected override void OnSessionClosed(CloseReason reason)
{
//add you logics which will be executed after the session is closed
base.OnSessionClosed(reason);
}
}
}
这么写 然后 主程序
using Socket.appReceive;
using Socket.appServer;
using Socket.appSession;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Socket
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to start the server!");
Console.ReadKey();
Console.WriteLine();
var appServer = new MyAppServer();
appServer.NewSessionConnected += new SessionHandler<MyAppSession>(appServer_NewSessionConnected);
appServer.NewRequestReceived += new RequestHandler<MyAppSession, StringRequestInfo>(appServer_NewRequestReceived);
//Setup the appServer
if (!appServer.Setup(2012)) //Setup with listening port
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
Console.WriteLine();
//Try to start the appServer
if (!appServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("The server started successfully, press key 'q' to stop it!");
while (Console.ReadKey().KeyChar != 'q')
{
Console.WriteLine();
continue;
}
//Stop the appServer
appServer.Stop();
Console.WriteLine("The server was stopped!");
Console.ReadKey();
}
/// <summary>
/// 对接收的数据进行处理
/// </summary>
/// <param name="session"></param>
/// <param name="requestInfo"></param>
private static void appServer_NewRequestReceived(MyAppSession session, StringRequestInfo requestInfo)
{
try
{
switch (requestInfo.Key.ToUpper())
{
case ("ECHO"):
if(requestInfo.Body.Contains("hello"))
session.Send("hi my name is Server");
else
session.Send("ninainaider");
break;
case ("ADD"):
new ADD().ExecuteCommand(session,requestInfo);
break;
case ("MULT"):
new MULT().ExecuteCommand(session, requestInfo);
break;
}
}
catch (Exception)
{
Console.WriteLine("异常");
}
}
static void appServer_NewSessionConnected(MyAppSession session)
{
session.Send("Welcome to SuperSocket Telnet Server");
}
}
}
这是修改过的 主程序
就是把原来的APPSERVER 和APPSESSIOn
变成了MYAPPSERVER和MYAPPSESSION罢了
当然我们的加法和乘法可不能忘 也会报错滴
改一下
ADD
using Socket.appSession;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Socket.appReceive
{
public class ADD : CommandBase<MyAppSession, StringRequestInfo>
{
public override void ExecuteCommand(MyAppSession session, StringRequestInfo requestInfo)
{
try
{
session.Send(requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString());
}
catch (Exception)
{
Console.WriteLine("加法异常");
}
}
}
}
MULT
using Socket.appSession;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Socket.appReceive
{
public class MULT : CommandBase<MyAppSession, StringRequestInfo>
{
public override void ExecuteCommand(MyAppSession session, StringRequestInfo requestInfo)
{
try
{
var result = 1;
foreach (var factor in requestInfo.Parameters.Select(p => Convert.ToInt32(p)))
{
result *= factor;
}
session.Send(result.ToString());
}
catch (Exception)
{
Console.WriteLine("乘法异常");
}
}
}
}
OK拉 以后扩展可以再自己的server 和session去扩展咯
(7条消息) SuperSocket教程四:实现自己的AppServer和AppSession_亮大大大的博客-CSDN博客_supersocket