Skip to main content
Version: 3.1.0

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 when useConnection is enabled

Properties

  • bool IsOpened
    Return true when connection be connected, and false when connection isn't connected.

  • Host Host
    Host class is endpoint container, and contain (ip address, port, ip type), it's endpoint metadata.

  • int Timeout
    It's timeout value and is used for determine connection timeout, timeout is milliseconds and only be used when UseConnection is enabled (true)

  • bool UseConnection
    It's determine if netly can use connection abstraction, if enabled (true) netly will send frequently ping message, and timeout for detect connection is alive.

  • string UUID
    UUID or (Unique User Identifier) is a unique string value that link a user, only server-side client.

Methods

Triggers

  • void Open(Host host)
    Open connection, if connection not be open will call and expose exception on OnError callback.
    • Host Netly host instance (endpoint metadata)

  • void Close()
    Close connection, if you need close connection use this method.

  • void ToData(byte[] buffer)
    void ToData(string buffer)
    Send raw buffer to server, buffer is string or byte[] (bytes).

  • void ToEvent(string name, byte[] buffer)
    void ToEvent(string name, string buffer)
    Send event (netly-event) to server, name is event identifier, buffer is event buffer (data), buffer is string or byte[] (bytes), if send buffer as string, netly will use NE.Default as encoding protocol.

Callbacks

  • void OnOpen(Action callback)
    Event responsible for receiving the connection opening information successfully
    • callback is the "function" responsible for handling the received event.

  • void OnError(Action<Exception> callback)
    Event responsible for receiving an error when opening a connection, the Exception contains the error information.
    • callback is the "function" responsible for handling the received event.

  • void OnClose(Action callback)
    Event responsible for receiving information about closing or terminating the connection with the server (UDP only when UseConnection is active (true))
    • callback is the "function" responsible for handling the received event.

  • void OnData(Action<byte[]> callback)
    Event responsible for receiving raw data (buffer) from the server
    • callback is the "function" responsible for handling the received event.

  • void OnEvent(Action<string, byte[]> callback)
    Event responsible for receiving events (netly-events) from the server
    • callback is the "function" responsible for handling the received event.

  • void OnModify(Action<Socket> socket)
    This event 'is responsible for executing modifications in Socket, this event is executed in the connection creation step, and you will have access to Socket 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();