プロトコルを実装する必要はありません。インターフェイスIReceiveFilter
を実装してReceiveFilter
を作成するだけです。
だから、最初の下のいずれかのようにカスタムRequestInfoをクラスを作成します。
public class MyRequestInfo : IRequestInfo
{
public string Key { get; set; }
public string Unicode { get; set; }
// You can add more properties here
}
その後ReceiveFilterを作成 - ReceiveFilterは基本的にすべての着信メッセージをフィルタリングするクラスです。これは、プロトコルを実装したくない場合に必要なものです。
public class MyReceiveFilter: IReceiveFilter<MyRequestInfo>
{
// This Method (Filter) is called whenever there is a new request from a connection/session
//- This sample method will convert the incomming Byte Array to Unicode string
public MyRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
{
rest = 0;
try
{
var dataUnicode = Encoding.Unicode.GetString(readBuffer, offset, length);
var deviceRequest = new MyRequestInfo { Unicode = dataUnicode };
return deviceRequest;
}
catch (Exception ex)
{
return null;
}
}
public void Reset()
{
throw new NotImplementedException();
}
public int LeftBufferSize { get; }
public IReceiveFilter<MyRequestInfo> NextReceiveFilter { get; }
public FilterState State { get; }
}
次のステップは、カスタムAppSession
を作成することです。セッションとは、クライアントがサーバーに接続してセッションを作成したときのようなもので、クライアントの接続が切断されたときやサーバーが接続を終了したときに破棄されます。これは、クライアントの接続が必要な状況で、サーバーが次のメッセージを送信するための確認応答を送信する必要がある場合に適しています。
public class MyAppSession : AppSession<MyAppSession, MyRequestInfo>
{
// Properties related to your session.
public int ClientKey { get; set; }
public string SomeProperty { get; set; }
}
最後のステップは、あなたはまた、同様のAppServerクラスの他のメソッドをオーバーライドすることができ、カスタムAppServer
// Here you will be telling the AppServer to use MyAppSession as the default AppSession class and the MyRequestInfo as the defualt RequestInfo
public class MyAppServer : AppServer<MyAppSession, MyRequestInfo>
{
// Here in constructor telling to use MyReceiveFilter and MyRequestInfo
protected MyAppServer() : base(new DefaultReceiveFilterFactory<MyReceiveFilter, MyRequestInfo>())
{
NewRequestReceived += ProcessNewMessage;
}
// This method/event will fire whenever a new message is received from the client/session
// After passing through the filter
// the requestInfo will contain the Unicode string
private void ProcessNewMessage(MyAppSession session, MyRequestInfo requestinfo)
{
session.ClientKey = SessionCount;
// Here you can access the Unicode strings that where generated in the MyReceiveFilter.Filter() Method.
Console.WriteLine(requestinfo.Unicode);
// Do whatever you want
session.Send("Hello World");
session.Close();
}
}
を作成することです:OnSessionClosed
かOnNewSessionConnected
それだこと - そして、あなただけの持っていますサーバを初期化して起動する:
var myAppServer = new MyAppServer();
if (!myAppServer.Setup(2012))
{
_logger.LogMessage(MessageType.Error, string.Format("Failed to setup server"));
return;
}
if (!myAppServer.Start())
{
_logger.LogMessage(MessageType.Error, string.Format("Failed to start server"));
return;
}
プロトコルを実装しないとどういう意味ですか?スーパーソケットは、設定に応じてTCPまたはUDPのいずれかを使用します –
まあ、明らかに、トランスポートレベルのプロトコルとしてTCPとUDPを使用します。しかし、ドキュメントを読むことで、データをカプセル化するための通信プロトコルを開発する必要があるようです。 あなたが[ここ](http://docs.supersocket.net/v1-6/en-US/The-Built-in-Command-Line-Protocol)を見れば、私の意味を読むことができます。このページの最初の質問には、あなたの答えがあります。私はあなたのアプリケーションレベルのプロトコルを定義する必要があります... _、これは必須と思われます。 ありがとうございます。 – tedebus
データをバイト単位で受信したいだけですか? –