-
Notifications
You must be signed in to change notification settings - Fork 0
Creating A Client
- Make sure to add the library as a reference to your C# project
- Include a using tag to the library at the top of any classes that use it
using com.benjaminapplegate.EasyNetworking;- Create an instance of the EasyClient class, it is recommended to make it accessible to other classes in your project so you can send messages to the server from anywhere in your project.
public static EasyClient client
static void Main(string[] args){
client= new EasyClient(serverIp, port);
}The serverIp property specifies the IP adress of the server to connect to and the port property specifies the port that the server is hosted on.
- Define what happens when the client receives a packet.
Every packet in this library has a type attached to it, when a packet is sent, the client or server specifies what type it is. On our client we define a function that gets called whenever a specific type of packet is received
public static EasyClient client
static void Main(string[] args){
client = new EasyClient(serverIp, port);
client.AddPacketHandler(packetType, handlerFunction);
}
public static void handlerFunction(Packet packet){
//handle data here
}The packetType parameter is an integer that is the type of packet the server sent, these are defined by the server that sent the packet. The handlerFunction parameter is a delegate to a function that takes a the packet as a parameter. We will cover Reading and Writing to and from a packet later in this tutorial.
- Connect to the server
public static EasyClient client
static void Main(string[] args){
client = new EasyClient(serverIp, port);
client.AddPacketHandler(packetType, handlerFunction);
client.ConnectToServer();
}- Disconnect the client
public static EasyClient client
static void Main(string[] args){
client = new EasyClient(serverIp, port);
client.AddPacketHandler(packetType, handlerFunction);
client.ConnectToServer();
while(condition){
//Code to run while the client is connected to the server
}
client.Disconnect();
}client.SendPacketToServer(packet);This function sends the specified packet to the server.
public static EasyClient client;
public static void OnClientConnection(){
//Handle client connection
}
static void Main(string[] args){
client= new EasyClient(serverIp, port);
client.SuccessfulClient= OnClientConnection;
client.ConnectToServer();
}The SuccessfulClient variable on the client class holds a delegate to a function that gets called when the client successfully connects to the server.
public static EasyClient client;
public static void OnClientFailedToConnect(){
//Handle client connection
}
static void Main(string[] args){
client= new EasyClient(serverIp, port);
client.FailedClient= OnClientConnection;
client.ConnectToServer();
}The FailedClient variable on the client class holds a delegate to a function that gets called when the client fails to connect to the server
The packet class has simple functions to read and write most primitive objects, these include:
intfloatdoubleboolcharshortlongstring
The read functions return the datatype specified by reading that data from the packet, and then preparing the packet to read the next data in the packet. Note that packets must be read in the order in which they were written, For Example: if a string was written into a packet and then an int, you should not read an int then a string, you must read the string first and the int second.
The write functions do not return anything and they take the data to be written as a parameter.
Reading an integer
int something = packet.ReadInt(); Writing a string
packet.WriteString("Text to write");