UdpClient
Udp client implementation
Namespace
using Netly;
Constructors
-
UdpClient UdpClient(bool useConnection ,int? timeout )useConnection
"enable or disable udp connection (using ping)timeout
is connection timeout (only whenuseConnection
is enabled
Properties
-
Returnbool IsOpenedtrue
when connection be connected, andfalse
when connection isn't connected.
-
Host HostHost class
is 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 Timeoutmilliseconds
and only be used whenUseConnection
is enabled (true
)
-
It's determine if netly can use connection abstraction, if enabled (bool UseConnectiontrue
) netly will send frequently ping message, andtimeout
for detect connection is alive.
-
string UUIDUUID
or (Unique User Identifier) is a unique string value that link a user, onlyserver-side
client.
Methods
Triggers
-
Open connection, if connection not be open will call and expose exception onvoid Open(Host host )OnError
callback.-
Host
Netly 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 )buffer
isstring
orbyte[]
(bytes).
-
Send event (netly-event) to server,void ToEvent(string name ,byte[] buffer )
void ToEvent(string name ,string buffer )name
is event identifier,buffer
is event buffer (data), buffer isstring
orbyte[]
(bytes), if send buffer as string, netly will useNE.Default
as encoding protocol.
Callbacks
-
Event responsible for receiving the connection opening information successfullyvoid OnOpen(Action callback )-
callback
is the "function" responsible for handling the received event.
-
-
Event responsible for receiving an error when opening a connection, thevoid OnError(Action<Exception> callback )Exception
contains the error information.-
callback
is 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 )UseConnection
is active (true
))-
callback
is the "function" responsible for handling the received event.
-
-
Event responsible for receiving raw data (buffer) from the servervoid OnData(Action<byte[]> callback )-
callback
is the "function" responsible for handling the received event.
-
-
Event responsible for receiving events (netly-events) from the servervoid OnEvent(Action<string, byte[]> callback )-
callback
is 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 toSocket
that will be used internally-
callback
is 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();