当前位置: 移动技术网 > IT编程>开发语言>c# > C#SuperSocket的搭建并配置启动总结

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

之前我们借助一个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 supersocket.socketbase.command;
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 supersocket2.command
{
   /// <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 system.componentmodel;
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框架搭建成功

以上就是全部知识点内容,感谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网