欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#SuperSocket的搭建并配置啟動(dòng)總結(jié)

 更新時(shí)間:2019年05月26日 14:38:15   投稿:laozhang  
在本篇文章里我們給大家總結(jié)了關(guān)于C#SuperSocket的搭建并配置啟動(dòng)的相關(guān)內(nèi)容,正在學(xué)習(xí)的朋友們跟著參考下。

之前我們借助一個(gè)SuperSocket實(shí)現(xiàn)了一個(gè)簡易版的服務(wù)器, 但是不管是Server還是Session都是使用框架的,本篇博客我們要實(shí)現(xiàn)自己的Server和Session,來重寫框架原生的Server或Session的方法,或添加自己所需的屬性,來實(shí)現(xiàn)自己的業(yè)務(wù)邏輯,并且也不在使用事件來綁定接收,連接,或關(guān)閉事件,全部交給Bootstrap來執(zhí)行,(這個(gè)Bootstrap并不是指前端框架的Bootstrap ,而是指的SuperSocket框架的一個(gè)引導(dǎo)程序或說是輔助程序),就是這里我們會使用Bootstrap 來配置啟動(dòng)SuperSocket;

本篇文章皆為我閱讀官方文檔后總結(jié)實(shí)現(xiàn),所以很多代碼是直接搬的官方文檔的,我的主要目的是為了能實(shí)現(xiàn)并運(yùn)行SuperSocket服務(wù)器,所以建議優(yōu)先閱讀官方文檔

官方文檔:http://docs.supersocket.net/v1-6/zh-CN

SuperSocket 是一個(gè)輕量級, 跨平臺而且可擴(kuò)展的 .Net/Mono Socket 服務(wù)器程序框架。你無須了解如何使用 Socket, 如何維護(hù) Socket 連接和 Socket 如何工作,但是你卻可以使用 SuperSocket 很容易的開發(fā)出一款 Socket 服務(wù)器端軟件,例如游戲服務(wù)器,GPS 服務(wù)器, 工業(yè)控制服務(wù)和數(shù)據(jù)采集服務(wù)器等等。

怎么從NuGet安裝SuperSocket就不再贅述了,我們直接看實(shí)現(xiàn)

首先我們可以按自己需求定義自己APPSession(因?yàn)槲乙膊恢牢易约憾x的Session中應(yīng)該有什么方法,什么屬性,所以照搬官方文檔了~~~)

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上一篇文章我們也說道,它默認(rèn)的請求的 key 和 body 通過字符 ' '  空格分隔, 因需求不同 我們可以將它改為 ':' 分隔 ,而且多個(gè)參數(shù)被字符 ',' 分隔,所以我們在修改了無參構(gòu)造函數(shù),來實(shí)現(xiàn)拓展命令行協(xié)議;

接下來要做的

所以我們來自己寫一個(gè)命令類

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" 的值用于匹配接收到請求實(shí)例(requestInfo)的Key。當(dāng)一個(gè)請求實(shí)例(requestInfo) 被收到時(shí),SuperSocket 將會通過匹配請求實(shí)例(requestInfo)的Key和命令的Name的方法來查找用于處理該請求的命令

但是由于類名的命名必須有字母數(shù)字下劃線組成,且數(shù)字不能開頭,如果要接收請求的Key為6003,我們就需要做一些修改

所以這里我重寫了Name方法,這樣,請求的Key是6003 也能觸發(fā)CommandOne命令

好了,我們的自定義Server,Session,命令都寫完了,接下來需要我們使用Bootstrap來配置啟動(dòng),我們這里只為了保證SuperSocket能正常啟動(dòng),所以不做多余的配置(☞配置示例)

修改App.config文件,添加<configuration>節(jié)點(diǎn)和<superSocket>節(jié)點(diǎn)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
  <section name="superSocket"
     type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" />
 </configSections>
 <superSocket>
  <servers>
   <!--serverType中,逗號左邊的是你自定義的server在項(xiàng)目中的位置,逗號右邊是項(xiàng)目名,ip就是服務(wù)器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>

配置完畢,我們啟動(dòng)程序,在Form_load中實(shí)例化bootstrap,啟動(dòng)服務(wù)(原諒我懶,實(shí)在不愿意對這個(gè)Form美化了,就加了一個(gè)Richtextbox,顯示一下是否初始化成功,啟動(dòng)成功)

 

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實(shí)例
      var bootstrap = BootstrapFactory.CreateBootstrap();
      //初始化
      if (!bootstrap.Initialize())
      {
        SetMessage("Failed to initialize!");
        return;
      }
      //開啟服務(wù)
      var result = bootstrap.Start();

      if (result == StartResult.Failed)
      {
        SetMessage("Failed to start!");
        
        return;
      }
      else
      {
        SetMessage("服務(wù)器啟動(dòng)成功");
      }
      //bootstrap.Stop();

    }

    public void SetMessage(string msg)
    {
      this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.AppendText(msg + "\r\n"); }));
    }

  }
}

好,一個(gè)簡單的,完整的自定義SuperSocket就完成了,我們運(yùn)行,借助TCP/UDP Socket調(diào)試工具執(zhí)行6003命令試一下

這里說明一下,SuperSocket框架的命令行協(xié)議定義了每個(gè)請求必須以回車換行結(jié)尾 "\r\n";

所以我們輸完6003:hello命令后,記得加回車;

測試完成,簡易SuperSocket框架搭建成功

以上就是全部知識點(diǎn)內(nèi)容,感謝大家對腳本之家的支持。

相關(guān)文章

最新評論