欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#SuperSocket的搭建并配置啟動總結

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

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

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

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

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

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

首先我們可以按自己需求定義自己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 通過字符 ' '  空格分隔, 因需求不同 我們可以將它改為 ':' 分隔 ,而且多個參數(shù)被字符 ',' 分隔,所以我們在修改了無參構造函數(shù),來實現(xiàn)拓展命令行協(xié)議;

接下來要做的

所以我們來自己寫一個命令類

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的方法來查找用于處理該請求的命令

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

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

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

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

<?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調試工具執(zhí)行6003命令試一下

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

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

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

以上就是全部知識點內(nèi)容,感謝大家對我們的支持。

上一篇:幾分鐘搞懂c#之FileStream對象讀寫大文件(推薦)

欄    目:C#教程

下一篇:C#的自定義語法糖的使用詳解

本文標題:C#SuperSocket的搭建并配置啟動總結

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4760.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有