之前我們借助一個SuperSocket實現了一個簡易版的服務器, 但是不管是Server還是Session都是使用框架的,本篇博客我們要實現自己的Server和Session,來重寫框架原生的Server或Session的方法,或添加自己所需的屬性,來實現自己的業務邏輯,並且也不在使用事件來綁定接收,連接,或關閉事件,全部交給Bootstrap來執行,(這個Bootstrap並不是指前端框架的Bootstrap ,而是指的SuperSocket框架的一個引導程序或說是輔助程序),就是這里我們會使用Bootstrap 來配置啟動SuperSocket;
本篇文章皆為我閱讀官方文檔后總結實現,所以很多代碼是直接搬的官方文檔的,我的主要目的是為了能實現並運行SuperSocket服務器,所以建議優先閱讀官方文檔
官方文檔:http://docs.supersocket.net/v1-6/zh-CN
SuperSocket 是一個輕量級, 跨平台而且可擴展的 .Net/Mono Socket 服務器程序框架。你無須了解如何使用 Socket, 如何維護 Socket 連接和 Socket 如何工作,但是你卻可以使用 SuperSocket 很容易的開發出一款 Socket 服務器端軟件,例如游戲服務器,GPS 服務器, 工業控制服務和數據采集服務器等等。
怎么從NuGet安裝SuperSocket就不再贅述了,我們直接看實現
首先我們可以按自己需求定義自己APPSession(因為我也不知道我自己定義的Session中應該有什么方法,什么屬性,所以照搬官方文檔了~~~)

1 using SuperSocket.SocketBase; 2 using SuperSocket.SocketBase.Protocol; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace SuperSocket2.Session 10 { 11 public class MySession : AppSession<MySession> 12 { 13 protected override void OnSessionStarted() 14 { 15 this.Send("Welcome to SuperSocket Telnet Server"); 16 } 17 18 protected override void HandleUnknownRequest(StringRequestInfo requestInfo) 19 { 20 this.Send("Unknow request"); 21 } 22 23 protected override void HandleException(Exception e) 24 { 25 this.Send("Application error: {0}", e.Message); 26 } 27 28 protected override void OnSessionClosed(CloseReason reason) 29 { 30 //add you logics which will be executed after the session is closed 31 base.OnSessionClosed(reason); 32 } 33 } 34 }
接着按自己需求定義自己APPServer,

1 using SuperSocket.SocketBase; 2 using SuperSocket.SocketBase.Config; 3 using SuperSocket.SocketBase.Protocol; 4 using SuperSocket2.Session; 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace SuperSocket2.Server 12 { 13 public class MyServer : AppServer<MySession> 14 { 15 16 public MyServer() 17 : base(new CommandLineReceiveFilterFactory(Encoding.Default, new BasicRequestInfoParser(":", ","))) 18 { 19 20 } 21 22 protected override bool Setup(IRootConfig rootConfig, IServerConfig config) 23 { 24 return base.Setup(rootConfig, config); 25 } 26 27 protected override void OnStartup() 28 { 29 base.OnStartup(); 30 } 31 32 protected override void OnStopped() 33 { 34 base.OnStopped(); 35 } 36 } 37 }
自定義的APPserver,在繼承APPServer是的Session泛型,記得要更改為我們自定義的Session上一篇文章我們也說道,它默認的請求的 key 和 body 通過字符 ' ' 空格分隔, 因需求不同 我們可以將它改為 ':' 分隔 ,而且多個參數被字符 ',' 分隔,所以我們在修改了無參構造函數,來實現拓展命令行協議;
接下來要做的
所以我們來自己寫一個命令類

1 using SuperSocket.SocketBase.Command; 2 using SuperSocket.SocketBase.Protocol; 3 using SuperSocket2.Session; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Threading; 11 12 namespace SuperSocket2.Command 13 { 14 /// <summary> 15 /// 處理請求頭為6003的命令 16 /// </summary> 17 public class CommandOne : CommandBase<MySession, StringRequestInfo> 18 { 19 public override string Name 20 { 21 get 22 { 23 return "6003"; 24 } 25 } 26 27 public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo) 28 { 29 //向客戶端返回信息,已接受到6003命令 30 s.Send("Order 6003 received"); 31 } 32 33 } 34 } 35
請求處理代碼必須被放置於方法 "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>節點
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="superSocket" 5 type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" /> 6 </configSections> 7 <superSocket> 8 <servers> 9 <!--serverType中,逗號左邊的是你自定義的server在項目中的位置,逗號右邊是項目名,ip就是服務器ip,port端口號--> 10 <server name="TelnetServer" 11 serverType="SuperSocket2.Server.MyServer,SuperSocket2" 12 ip="Any" port="3666"> 13 </server> 14 </servers> 15 </superSocket> 16 <startup> 17 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 18 </startup> 19 </configuration>
配置完畢,我們啟動程序,在Form_load中實例化bootstrap,啟動服務(原諒我懶,實在不願意對這個Form美化了,就加了一個Richtextbox,顯示一下是否初始化成功,啟動成功)

1 using SuperSocket.SocketBase; 2 using SuperSocket.SocketEngine; 3 using System; 4 using System.Collections.Generic; 5 using System.ComponentModel; 6 using System.Data; 7 using System.Drawing; 8 using System.Linq; 9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 14 15 namespace SuperSocket2 16 { 17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 private void Form1_Load(object sender, EventArgs e) 24 { 25 //聲明bootStrap實例 26 var bootstrap = BootstrapFactory.CreateBootstrap(); 27 //初始化 28 if (!bootstrap.Initialize()) 29 { 30 SetMessage("Failed to initialize!"); 31 return; 32 } 33 //開啟服務 34 var result = bootstrap.Start(); 35 36 if (result == StartResult.Failed) 37 { 38 SetMessage("Failed to start!"); 39 40 return; 41 } 42 else 43 { 44 SetMessage("服務器啟動成功"); 45 } 46 //bootstrap.Stop(); 47 48 } 49 50 public void SetMessage(string msg) 51 { 52 this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.AppendText(msg + "\r\n"); })); 53 } 54 55 } 56 }
好,一個簡單的,完整的自定義SuperSocket就完成了,我們運行,借助TCP/UDP Socket調試工具執行6003命令試一下
這里說明一下,SuperSocket框架的命令行協議定義了每個請求必須以回車換行結尾 "\r\n";
所以我們輸完6003:hello命令后,記得加回車;
測試完成,簡易SuperSocket框架搭建成功
以上為我自己學習總結並實現,有錯誤之處,希望大家不吝賜教,感謝(抱拳)!