C#SuperSocket的搭建并配置启动总结

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C#SuperSocket的搭建并配置启动总结
之前我们借助⼀个SuperSocket实现了⼀个简易版的服务器, 但是不管是Server还是Session都是使⽤框架的,本篇博客我们要实现⾃⼰的Server和Session,来重写框架原⽣的Server或Session的⽅法,或添加⾃⼰所需的属性,来实现⾃⼰的业务逻辑,并且也不在使⽤事件来绑定接收,连接,或关闭事件,全部交给Bootstrap来执⾏,(这个Bootstrap并不是指前端框架的Bootstrap ,⽽是指的SuperSocket框架的⼀个引导程序或说是辅助程序),就是这⾥我们会使⽤Bootstrap 来配置启动SuperSocket;
本篇⽂章皆为我阅读官⽅⽂档后总结实现,所以很多代码是直接搬的官⽅⽂档的,我的主要⽬的是为了能实现并运⾏SuperSocket服务器,所以建议优先阅读官⽅⽂档SuperSocket 是⼀个轻量级, 跨平台⽽且可扩展的 .Net/Mono Socket 服务器程序框架。

你⽆须了解如何使⽤ Socket, 如何维护 Socket 连接和 Socket 如何⼯作,但是你却可以使⽤ SuperSocket 很容易的开发出⼀款 Socket 服务器端软件,例如游戏服务器,GPS 服务器, ⼯业控制服务和数据采集服务器等等。

怎么从NuGet安装SuperSocket就不再赘述了,我们直接看实现
⾸先我们可以按⾃⼰需求定义⾃⼰APPSession(因为我也不知道我⾃⼰定义的Session中应该有什么⽅法,什么属性,所以照搬官⽅⽂档了~~~)
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SuperSocket2.Session
{
public class MySession : AppSession<MySession>
{
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);
}
}
}
接着按⾃⼰需求定义⾃⼰APPServer,
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using SuperSocket2.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SuperSocket2.Server
{
public class MyServer : AppServer<MySession>
{
public MyServer()
: base(new CommandLineReceiveFilterFactory(Encoding.Default, new BasicRequestInfoParser(":", ",")))
{
}
protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
{
return base.Setup(rootConfig, config);
}
protected override void OnStartup()
{
base.OnStartup();
}
protected override void OnStopped()
{
base.OnStopped();
}
}
}
⾃定义的APPserver,在继承APPServer是的Session泛型,记得要更改为我们⾃定义的Session上⼀篇⽂章我们也说道,它默认的请求的 key 和 body 通过字符 ' ' 空格分隔, 因需求不同我们可以将它改为 ':' 分隔 ,⽽且多个参数被字符 ',' 分隔,所以我们在修改了⽆参构造函数,来实现拓展命令⾏协议;
接下来要做的
所以我们来⾃⼰写⼀个命令类
using mand;
using SuperSocket.SocketBase.Protocol;
using SuperSocket2.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace mand
{
/// <summary>
/// 处理请求头为6003的命令
/// </summary>
public class CommandOne : CommandBase<MySession, StringRequestInfo>
{
public override string Name
{
get
{
return "6003";
}
}
public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)
{
//向客户端返回信息,已接受到6003命令
s.Send("Order 6003 received");
}
}
}
请求处理代码必须被放置于⽅法 "ExecuteCommand(TAppSession session, TRequestInfo requestInfo)" 之中,并且属性 "Name" 的值⽤于匹配接收到请求实例(requestInfo)的Key。

当⼀个请求实例(requestInfo) 被收到时,SuperSocket 将会通过匹配请求实例(requestInfo)的Key和命令的Name的⽅法来查找⽤于处理该请求的命令
但是由于类名的命名必须有字母数字下划线组成,且数字不能开头,如果要接收请求的Key为6003,我们就需要做⼀些修改
所以这⾥我重写了Name⽅法,这样,请求的Key是6003 也能触发CommandOne命令
好了,我们的⾃定义Server,Session,命令都写完了,接下来需要我们使⽤Bootstrap来配置启动,我们这⾥只为了保证SuperSocket能正常启动,所以不做多余的配置(☞配置⽰例)
修改App.config⽂件,添加<configuration>节点和<superSocket>节点
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="superSocket"
type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" />
</configSections>
<superSocket>
<servers>
<!--serverType中,逗号左边的是你⾃定义的server在项⽬中的位置,逗号右边是项⽬名,ip就是服务器ip,port端⼝号-->
<server name="TelnetServer"
serverType="SuperSocket2.Server.MyServer,SuperSocket2"
ip="Any" port="3666">
</server>
</servers>
</superSocket>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
配置完毕,我们启动程序,在Form_load中实例化bootstrap,启动服务(原谅我懒,实在不愿意对这个Form美化了,就加了⼀个Richtextbox,显⽰⼀下是否初始化成功,启动成功)
using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SuperSocket2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//声明bootStrap实例
var bootstrap = BootstrapFactory.CreateBootstrap();
//初始化
if (!bootstrap.Initialize())
{
SetMessage("Failed to initialize!");
return;
}
//开启服务
var result = bootstrap.Start();
if (result == StartResult.Failed)
{
SetMessage("Failed to start!");
return;
}
else
{
SetMessage("服务器启动成功");
}
//bootstrap.Stop();
}
public void SetMessage(string msg)
{
this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.AppendText(msg + "\r\n"); }));
}
}
}
好,⼀个简单的,完整的⾃定义SuperSocket就完成了,我们运⾏,借助TCP/UDP Socket调试⼯具执⾏6003命令试⼀下
这⾥说明⼀下,SuperSocket框架的命令⾏协议定义了每个请求必须以回车换⾏结尾 "\r\n";
所以我们输完6003:hello命令后,记得加回车;
测试完成,简易SuperSocket框架搭建成功
以上就是全部知识点内容,感谢⼤家对的⽀持。

相关文档
最新文档