UdpClient
Udp client implementation
Namespace
using Netly;
Constructors
-
UdpClient UdpClient(bool useConnection ,int? timeout )useConnection"enable or disable udp connection (using ping)timeoutis connection timeout (only whenuseConnectionis enabled
Properties
-
Returnbool IsOpenedtruewhen connection be connected, andfalsewhen connection isn't connected.
-
Host HostHost classis endpoint container, and contain (ip address, port, ip type), it's endpoint metadata.
-
It's timeout value and is used for determine connection timeout, timeout isint Timeoutmillisecondsand only be used whenUseConnectionis enabled (true)
-
It's determine if netly can use connection abstraction, if enabled (bool UseConnectiontrue) netly will send frequently ping message, andtimeoutfor detect connection is alive.
-
string UUIDUUIDor (Unique User Identifier) is a unique string value that link a user, onlyserver-sideclient.
Methods
Triggers
-
Open connection, if connection not be open will call and expose exception onvoid Open(Host host )OnErrorcallback.-
HostNetly host instance (endpoint metadata)
-
-
Close connection, if you need close connection use this method.void Close()
-
Send raw buffer to server,void ToData(byte[] buffer )
void ToData(string buffer )bufferisstringorbyte[](bytes).
-
Send event (netly-event) to server,void ToEvent(string name ,byte[] buffer )
void ToEvent(string name ,string buffer )nameis event identifier,bufferis event buffer (data), buffer isstringorbyte[](bytes), if send buffer as string, netly will useNE.Defaultas encoding protocol.
Callbacks
-
Event responsible for receiving the connection opening information successfullyvoid OnOpen(Action callback )-
callbackis the "function" responsible for handling the received event.
-
-
Event responsible for receiving an error when opening a connection, thevoid OnError(Action<Exception> callback )Exceptioncontains the error information.-
callbackis the "function" responsible for handling the received event.
-
-
Event responsible for receiving information about closing or terminating the connection with the server (UDP only whenvoid OnClose(Action callback )UseConnectionis active (true))-
callbackis the "function" responsible for handling the received event.
-
-
Event responsible for receiving raw data (buffer) from the servervoid OnData(Action<byte[]> callback )-
callbackis the "function" responsible for handling the received event.
-
-
Event responsible for receiving events (netly-events) from the servervoid OnEvent(Action<string, byte[]> callback )-
callbackis the "function" responsible for handling the received event.
-
-
This event 'is responsible for executing modifications invoid OnModify(Action<Socket> socket )Socket, this event is executed in the connection creation step, and you will have access toSocketthat will be used internally-
callbackis the "function" responsible for handling the received event.
-
Example
using System;
using Netly;
using Netly.Core;
const string ip = "127.0.0.1";
const int port = 3000;
const int myTimeout = 10000; // 10s
Host host = new Host(ip, port);
UdpClient client = new UdpClient(useConnection: true, timeout: myTimeout);
client.OnOpen(() =>
{
// connection opened.
Console.WriteLine("Connection opened.");
// send raw data to the server
client.ToData(NE.GetBytes("Is raw data", NE.Mode.ASCII));
// send event to the server
client.ToEvent("hello" NE.GetBytes("Is event data", NE.Mode.ASCII));
});
client.OnError((exception) =>
{
// connection opened.
Console.WriteLine($"Connection error: {exception}.");
});
client.OnClose(() =>
{
// connection closed.
Console.WriteLine("Connection closed.");
});
client.OnData((buffer) =>
{
// raw data received
Console.WriteLine($"Raw data: {NE.GetString(buffer)}");
});
client.OnEvent((name, buffer) =>
{
// detect close message
if (name == "goodbye")
{
// receive goodbye event
Console.WriteLine($"Server sent goodbye -> {NE.GetString(buffer)}");
client.Close();
}
else
{
// event received
Console.WriteLine($"Event: {name} -> {NE.GetString(buffer)}");
}
});
// Open connection
client.Open(host);
// Block main thread for not end program (console application)
Console.ReadLine();