1. 创建自定义类MySession,继承AppSession类并重写AppSession类的方法
注:一个AppSession对象对应一个连接
-
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
-
{
-
-
-
-
public class MySession : AppSession<MySession>
-
{
-
-
-
-
protected override void OnSessionStarted()
-
{
-
<span style="white-space:pre"> </span>
-
Console.WriteLine(this.LocalEndPoint.Address.ToString());
-
this.Send("\n\rHello User");
-
}
-
-
-
-
-
-
protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
-
{
-
this.Send("\n\r未知的命令");
-
}
-
-
-
-
-
-
protected override void HandleException(Exception e)
-
{
-
this.Send("\n\r异常: {0}", e.Message);
-
}
-
-
-
-
-
-
protected override void OnSessionClosed(CloseReason reason)
-
{
-
base.OnSessionClosed(reason);
-
}
-
}
-
}
2. 创建自定义类MyServer,继承AppServer类并重写AppServer类的方法
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using SuperSocket.SocketBase;
-
using SuperSocket.SocketBase.Config;
-
-
namespace SuperSocketApp1
-
{
-
-
-
-
public class MyServer : AppServer<MySession>
-
{
-
protected override void OnStartup()
-
{
-
base.OnStartup();
-
Console.WriteLine("服务器已启动");
-
}
-
-
-
-
-
-
protected override void OnNewSessionConnected(MySession session)
-
{
-
base.OnNewSessionConnected(session);
-
<span style="white-space:pre"> </span>
-
Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":连接");
-
}
-
-
-
-
-
-
-
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);">
-
Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":断开连接");
-
}
-
-
protected override void OnStopped()
-
{
-
base.OnStopped();
-
Console.WriteLine("服务器已停止");
-
}
-
}
-
}
3. 创建自定义命令HELLO类,继承CommandBase并重写ExecuteCommand方法
-
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
-
{
-
-
-
-
public class HELLO : CommandBase<MySession, StringRequestInfo>
-
{
-
-
-
-
-
-
public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)
-
{
-
session.Send("Hello World!");
-
}
-
}
-
}
4. 修改Main方法代码
-
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
原文:http://blog.csdn.net/xiaoweiserver/article/details/39483111