[SetNet&Console Application]最初のステップ。 SetNet.PeerBase。 パート2

こんにちは、今日はSetNetサーバーネットワークソリューションのチュートリアルコースを続けます 。 今日は、クライアントのクラスと、顧客から受信したデータの処理について説明します。 始めましょう。

Peer 」というクラスを作成してから、名前空間「 SetNet 」を接続し、基本クラス「 PeerBase 」からクラス「 Peer 」を継承しましょう。 クラスは次のようになります。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SetNet; using SetNet; namespace Server { class Peer:PeerBase { } } 


次に、 Peerクラスのコンストラクターを作成します。 次のようになります。

 public Peer(ClientInfo info) : base(info) { } 


この場合、コンストラクターは、クライアント情報が格納されているClientInfoクラスのインスタンスを受け入れます。
次に、「 PeerBase 」から抽象クラスを実装する必要があります。その後、クラスは次のようになります。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SetNet; namespace ServerTest { class Peer:PeerBase { public override void ReceiveData(EventData data) { throw new NotImplementedException(); } public override void ClientDisconected() { throw new NotImplementedException(); } } } 


ReceiveData 」メソッドは、クライアントからのメッセージと対話します。

ReceiveData 」メソッドを検討してください。



EventData 」には、すべての送信データを含む「 dic 」と呼ばれるディクショナリが含まれています。
新しいNotImplementedException()をスローします。 この行は不要なので削除できます。 抽象クラスの実装後に作成されます。

ReceiveData 」メソッドの「 EventData 」オブジェクトとの相互作用を検討してください。

まず、イベントを処理する必要があります。 これを行うには、整理します。
 switch (data.eventCode) { case 1: Console.WriteLine("Received message from the client with {0} eventCode. Message {1}",data.eventCode,data.dic[1].ToString()); break; default: Console.WriteLine("Received unknown message from the client with {0} eventCode. Message {1}",data.eventCode,data.dic[1].ToString()); break; } 


ここでは、メッセージコードと出力による検索を確認できます。 次に、クライアントへのサーバー応答を作成します。 これを行うには、「 EventData 」クラスの新しいインスタンスを作成し、クラスコンストラクターの前に「 response 」という名前を付けます。 次に、「 ReceiveData 」メソッドで、「 case 1 」の回答を作成します。
 EventData response = new EventData(1, new Dictionary<int, object> { { 1, "Response from the server" } }); 

答えを考慮してください:

 SendEvent(response); 


SendEventメソッドは、 EventDataクラスのインスタンスを受け取り、クライアントに送信します。
変更後のReceiveDataメソッド:
 public override void ReceiveData(EventData data) { switch (data.eventCode) { case 1: Console.WriteLine("Received message from the client with {0} eventCode. Message {1}", data.eventCode, data.dic[1].ToString()); EventData response = new EventData(1, new Dictionary<int, object> { { 1, "Response from the server" } }); SendEvent(response); break; default: Console.WriteLine("Received unknown message from the client with {0} eventCode. Message {1}", data.eventCode, data.dic[1].ToString()); break; } } 


GameServer 」クラスに「 Peer 」クラスのインスタンスを作成するだけです。「 GameServer 」クラスに移りましょう。 「 NewClient 」メソッドで、「 throw new NotImplementedException(); 」という行を削除します。NewClient 」メソッドで、「 Peer 」クラスのインスタンスを作成します。

これでメソッドは次のようになります。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SetNet; namespace Server { public class GameServer:SetNet.Server { public GameServer() : base() { } public override void NewClient(ClientInfo info) { Peer peer = new Peer(info); } } } 

これでこのレッスンは終了です。 ご清聴ありがとうございました。 ご質問がある場合は、rebegin @ list.ruまたはskype haker954までご連絡ください。

Source: https://habr.com/ru/post/J242017/


All Articles