πŸ“œ ⬆️ ⬇️

[SetNet & Console Application] First steps. SetNet.PeerBase. Part 2

Hello, today we continue the course of lessons about the SetNet Server network solution. And today we will talk about the client's class and data processing, which are accepted from customers. Let's start.

Create a class called β€œ Peer ”, then connect the namespace β€œ SetNet β€œ and inherit the class β€œ Peer ” from the base class β€œ PeerBase β€œ. The class should have the following form:

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 { } } 


Now we will create a constructor of the β€œ Peer ” class. It should be ironed like this:
')
 public Peer(ClientInfo info) : base(info) { } 


In this case, the constructor accepts an instance of the ClientInfo class in which information about the client is stored.
Next, you need to implement an abstract class from β€œ PeerBase ”, after which the class will look like:

 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(); } } } 


The β€œ ReceiveData ” method interacts with messages from the client.

Consider the β€œ ReceiveData β€œ method:



β€œ EventData ” contains a Dictionary with the name β€œ dic ” which contains all the data transferred.
throw new NotImplementedException (); can be deleted because this line is not needed. It is created after the implementation of an abstract class.

Consider the interaction with the β€œ EventData ” object in the β€œ ReceiveData β€œ method:

First you need to handle the event. To do this, do a bust:
 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; } 


Here you can see the search by message code and output. Next, create a server response to the client. To do this, create a new instance of the β€œ EventData ” class and give it the name β€œ response ” in front of the class constructor. Next, in the β€œ ReceiveData ” method, we will create the answer in β€œ case 1 ”.
 EventData response = new EventData(1, new Dictionary<int, object> { { 1, "Response from the server" } }); 

Consider the answer:

 SendEvent(response); 


The β€œ SendEvent ” method takes an instance of the β€œ EventData ” class and sends it to the client.
β€œ ReceiveData ” method after changes:
 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; } } 


It only remains to create an instance of the class β€œ Peer ” in the class β€œ GameServer β€œ: Let’s go to the class β€œ GameServer β€œ. In the β€œ NewClient ” method, remove the line β€œ throw new NotImplementedException (); ”In theβ€œ NewClient ”method, create an instance of theβ€œ Peer β€œclass.

Now the method will look like this:

 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); } } } 

At the end of this lesson. Thanks for attention. If you have any questions, write to rebegin@list.ru or skype haker954.

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


All Articles