Added nicknames

This commit is contained in:
2025-08-21 18:55:11 +02:00
parent 98aa0467b1
commit 32c2491d25
9 changed files with 145 additions and 95 deletions

View File

@@ -34,7 +34,6 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
## Server example:
using EonaCat.Connections;
using EonaCat.Connections.Models;
namespace EonaCat.Connections.Server.Example
@@ -71,11 +70,12 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
var config = new Configuration
{
Protocol = ProtocolType.TCP,
Port = 8080,
UseSsl = true,
Port = 1111,
UseSsl = false,
UseAesEncryption = true,
MaxConnections = 100000,
ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss"),
AesPassword = "EonaCat.Connections.Password",
//Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss")
};
_server = new NetworkServer(config);
@@ -89,7 +89,14 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
_server.OnDataReceived += async (sender, e) =>
{
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
if (e.HasNickname)
{
Console.WriteLine($"Received from {e.Nickname}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
}
else
{
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
}
// Echo back the message
if (e.IsBinary)
@@ -103,7 +110,16 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
};
_server.OnDisconnected += (sender, e) =>
Console.WriteLine($"Client {e.ClientId} disconnected");
{
if (e.HasNickname)
{
Console.WriteLine($"Client {e.Nickname} disconnected");
}
else
{
Console.WriteLine($"Client {e.ClientId} disconnected");
}
};
await _server.StartAsync();
}
@@ -114,6 +130,7 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
using EonaCat.Connections;
using EonaCat.Connections.Models;
using System.Text;
namespace EonaCat.Connections.Client.Example
{
@@ -121,9 +138,9 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
{
private static NetworkClient _client;
public static void Main(string[] args)
public static async Task Main(string[] args)
{
CreateClientAsync().ConfigureAwait(false);
await CreateClientAsync().ConfigureAwait(false);
while (true)
{
@@ -131,13 +148,13 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
var message = Console.ReadLine();
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
_client.DisconnectAsync().ConfigureAwait(false);
await _client.DisconnectAsync().ConfigureAwait(false);
break;
}
if (!string.IsNullOrEmpty(message))
{
_client.SendAsync(message).ConfigureAwait(false);
await _client.SendAsync(message).ConfigureAwait(false);
}
}
}
@@ -148,23 +165,28 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
{
Protocol = ProtocolType.TCP,
Host = "127.0.0.1",
Port = 8080,
UseSsl = true,
Port = 1111,
UseSsl = false,
UseAesEncryption = true,
ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("client.pfx", "p@ss"),
AesPassword = "EonaCat.Connections.Password",
//Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("client.pfx", "p@ss"),
};
_client = new NetworkClient(config);
// Subscribe to events
_client.OnConnected += (sender, e) =>
{
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
};
_client.OnDataReceived += (sender, e) =>
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
_client.OnDisconnected += (sender, e) =>
{
Console.WriteLine("Disconnected from server");
};
await _client.ConnectAsync();
@@ -175,4 +197,4 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
await _client.SendAsync("Hello server!");
}
}
}
}