TcpClient
Tcp client implementation
Namespace
using Netly;
Constructors
-
TcpClient TcpClient(bool framing )framing
Enable or disableNetly.Core.MessageFraming
.true
Netly will use its own message framing protocol.false
You will receive raw stream data without framing middleware (for receive raw data useTcpClient.OnData(Action<byte[]> callback)
)
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.
-
Return true when the instance is usingbool FramingMessageFraming protocol
connection and false when the connection is not usingMessageFraming protocol
-
Return true when the instance is usingbool IsEncryptedTLS/SSL
and false when isn't using this.
-
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)
-
-
Enable TLS/SSL connection onvoid UseEncryption(bool enableEncryption ,Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> onValidation = null )client side
. (Must used beforeTcpClient.Open(Host host)
)-
enableEncryption
Settrue for enableTLS/SSL
and setfalse don't enableSSL/TLS
-
onValidation
Is aFunc
that will executed for validate server certificate. Default behaviour returntrue
without validate any argument. See examples onTLS/SSL
session.
-
-
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.void OnClose(Action callback )-
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 (dotnet >= 6.0)
using System;
using Netly;
using Netly.Core;
int pingCounter = 0;
Host host = new Host("127.0.0.1", 8080);
TcpClient client = new TcpClient(framing: true);
// Enable TLS/SSL
// onValidation is optional, default behaviour return true without check any data.
client.UseConnection(enableConnection: true, onValidation: (sender, cert, chain, policyErrors) =>
{
#if DEBUG
/*
Default behaviour, Ignore certificate validation.
*/
return true;
#else
/*
Custom validation callback;
*/
if (sslPolicyErrors == SslPolicyErrors.None) return true;
// ignore chain errors as where self signed
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors) return true;
// Invalid SSL connection
return false;
#endif
});
client.OnOpen(() =>
{
// connection opened
client.ToEvent("welcome", "hello server!");
Console.WriteLine("Client connected!");
});
client.OnError((e) =>
{
// called when connection not opened or server isn't using framing protocol (if framing is enabled)
Console.WriteLine($"Connection error: {e}");
});
client.OnClose(() =>
{
Console.WriteLine("Client disconnected!");
});
client.OnEvent((name, data) =>
{
name = name.ToLower().Trim(); // convert string to lower case
if(name == "ping")
{
pingCounter++;
client.ToEvent("pong", $"ping counter: {pingCounter}");
}
else if(name == "q" || name == "quit" || name == "done")
{
client.Close();
}
Console.WriteLine($"Client event ({name}) -> {NE.GetString(data)}");
});
// open connection
client.Open(host);
// block main thread
bool running = false;
white(running)
{
Console.WriteLine("Netly ``TcpClient``\n\tq: Quit\n\tc: Close connection\n\tr: Reconnect");
string input = Console.ReadLine() ?? "";
switch(input)
{
case "q":
client.Close();
running = false;
break;
case "c":
client.Close();
break;
case "r":
client.Open(host);
break;
}
}
Console.WriteLine("Goodbye!!!");