Added nicknames
This commit is contained in:
parent
98aa0467b1
commit
32c2491d25
|
@ -46,13 +46,17 @@ namespace EonaCat.Connections.Client.Example
|
||||||
|
|
||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
_client.OnConnected += (sender, e) =>
|
_client.OnConnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
||||||
|
};
|
||||||
|
|
||||||
_client.OnDataReceived += (sender, e) =>
|
_client.OnDataReceived += (sender, e) =>
|
||||||
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
|
||||||
_client.OnDisconnected += (sender, e) =>
|
_client.OnDisconnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine("Disconnected from server");
|
Console.WriteLine("Disconnected from server");
|
||||||
|
};
|
||||||
|
|
||||||
await _client.ConnectAsync();
|
await _client.ConnectAsync();
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,14 @@ namespace EonaCat.Connections.Server.Example
|
||||||
|
|
||||||
_server.OnDataReceived += async (sender, 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
|
// Echo back the message
|
||||||
if (e.IsBinary)
|
if (e.IsBinary)
|
||||||
|
@ -67,7 +74,16 @@ namespace EonaCat.Connections.Server.Example
|
||||||
};
|
};
|
||||||
|
|
||||||
_server.OnDisconnected += (sender, 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();
|
await _server.StartAsync();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
<Copyright>EonaCat (Jeroen Saey)</Copyright>
|
<Copyright>EonaCat (Jeroen Saey)</Copyright>
|
||||||
<PackageReadmeFile>readme.md</PackageReadmeFile>
|
<PackageReadmeFile>readme.md</PackageReadmeFile>
|
||||||
<PackageId>EonaCat.Connections</PackageId>
|
<PackageId>EonaCat.Connections</PackageId>
|
||||||
<Version>1.0.3</Version>
|
<Version>1.0.4</Version>
|
||||||
<Authors>EonaCat (Jeroen Saey)</Authors>
|
<Authors>EonaCat (Jeroen Saey)</Authors>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageIcon>EonaCat.png</PackageIcon>
|
<PackageIcon>EonaCat.png</PackageIcon>
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace EonaCat.Connections.EventArguments
|
||||||
public class ConnectionEventArgs : EventArgs
|
public class ConnectionEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public string ClientId { get; set; }
|
public string ClientId { get; set; }
|
||||||
|
public string Nickname { get; set; }
|
||||||
|
public bool HasNickname => !string.IsNullOrEmpty(Nickname);
|
||||||
public IPEndPoint RemoteEndPoint { get; set; }
|
public IPEndPoint RemoteEndPoint { get; set; }
|
||||||
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
|
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,5 +11,6 @@ namespace EonaCat.Connections
|
||||||
public DateTime Timestamp { get; internal set; } = DateTime.UtcNow;
|
public DateTime Timestamp { get; internal set; } = DateTime.UtcNow;
|
||||||
public IPEndPoint RemoteEndPoint { get; internal set; }
|
public IPEndPoint RemoteEndPoint { get; internal set; }
|
||||||
public string Nickname { get; internal set; }
|
public string Nickname { get; internal set; }
|
||||||
|
public bool HasNickname => !string.IsNullOrEmpty(Nickname);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +0,0 @@
|
||||||
namespace EonaCat.Connections.EventArguments
|
|
||||||
{
|
|
||||||
public class NicknameConnectionEventArgs : ConnectionEventArgs
|
|
||||||
{
|
|
||||||
public string Nickname { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -61,7 +61,7 @@ namespace EonaCat.Connections
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var sslStream = new SslStream(stream, false, userCertificateValidationCallback:_config.GetRemoteCertificateValidationCallback());
|
var sslStream = new SslStream(stream, false, userCertificateValidationCallback: _config.GetRemoteCertificateValidationCallback());
|
||||||
if (_config.Certificate != null)
|
if (_config.Certificate != null)
|
||||||
{
|
{
|
||||||
sslStream.AuthenticateAsClient(_config.Host, new X509CertificateCollection { _config.Certificate }, _config.CheckCertificateRevocation);
|
sslStream.AuthenticateAsClient(_config.Host, new X509CertificateCollection { _config.Certificate }, _config.CheckCertificateRevocation);
|
||||||
|
@ -111,21 +111,24 @@ namespace EonaCat.Connections
|
||||||
public int Port => _config != null ? _config.Port : 0;
|
public int Port => _config != null ? _config.Port : 0;
|
||||||
private async Task ConnectUdp()
|
private async Task ConnectUdp()
|
||||||
{
|
{
|
||||||
try
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
_udpClient = new UdpClient();
|
try
|
||||||
_udpClient.Connect(_config.Host, _config.Port);
|
{
|
||||||
_isConnected = true;
|
_udpClient = new UdpClient();
|
||||||
|
_udpClient.Connect(_config.Host, _config.Port);
|
||||||
|
_isConnected = true;
|
||||||
|
|
||||||
OnConnected?.Invoke(this, new ConnectionEventArgs { ClientId = "self", RemoteEndPoint = new IPEndPoint(IPAddress.Parse(_config.Host), _config.Port) });
|
OnConnected?.Invoke(this, new ConnectionEventArgs { ClientId = "self", RemoteEndPoint = new IPEndPoint(IPAddress.Parse(_config.Host), _config.Port) });
|
||||||
|
|
||||||
// Start receiving data
|
// Start receiving data
|
||||||
_ = Task.Run(() => ReceiveUdpDataAsync(), _cancellation.Token);
|
_ = Task.Run(() => ReceiveUdpDataAsync(), _cancellation.Token);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Failed to connect UDP" });
|
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Failed to connect UDP" });
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -232,46 +235,49 @@ namespace EonaCat.Connections
|
||||||
|
|
||||||
private async Task ProcessReceivedDataAsync(byte[] data)
|
private async Task ProcessReceivedDataAsync(byte[] data)
|
||||||
{
|
{
|
||||||
try
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
// Data is already decrypted if AES is enabled
|
|
||||||
// Just update stats / handle string conversion
|
|
||||||
|
|
||||||
bool isBinary = true;
|
|
||||||
string stringData = null;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
stringData = Encoding.UTF8.GetString(data);
|
// Data is already decrypted if AES is enabled
|
||||||
if (Encoding.UTF8.GetBytes(stringData).Length == data.Length)
|
// Just update stats / handle string conversion
|
||||||
|
|
||||||
|
bool isBinary = true;
|
||||||
|
string stringData = null;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
isBinary = false;
|
stringData = Encoding.UTF8.GetString(data);
|
||||||
|
if (Encoding.UTF8.GetBytes(stringData).Length == data.Length)
|
||||||
|
{
|
||||||
|
isBinary = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Keep as binary
|
||||||
|
}
|
||||||
|
|
||||||
|
OnDataReceived?.Invoke(this, new DataReceivedEventArgs
|
||||||
|
{
|
||||||
|
ClientId = "server",
|
||||||
|
Data = data,
|
||||||
|
StringData = stringData,
|
||||||
|
IsBinary = isBinary
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (_config.UseAesEncryption)
|
||||||
|
{
|
||||||
|
OnEncryptionError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error processing data" });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error processing data" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
});
|
||||||
{
|
|
||||||
// Keep as binary
|
|
||||||
}
|
|
||||||
|
|
||||||
OnDataReceived?.Invoke(this, new DataReceivedEventArgs
|
|
||||||
{
|
|
||||||
ClientId = "server",
|
|
||||||
Data = data,
|
|
||||||
StringData = stringData,
|
|
||||||
IsBinary = isBinary
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
if (_config.UseAesEncryption)
|
|
||||||
{
|
|
||||||
OnEncryptionError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error processing data" });
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error processing data" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -401,16 +407,19 @@ namespace EonaCat.Connections
|
||||||
|
|
||||||
public async Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
{
|
{
|
||||||
_isConnected = false;
|
await Task.Run(() =>
|
||||||
_cancellation?.Cancel();
|
{
|
||||||
_tcpClient?.Close();
|
_isConnected = false;
|
||||||
_udpClient?.Close();
|
_cancellation?.Cancel();
|
||||||
_stream?.Dispose();
|
_tcpClient?.Close();
|
||||||
_aesEncryption?.Dispose();
|
_udpClient?.Close();
|
||||||
|
_stream?.Dispose();
|
||||||
|
_aesEncryption?.Dispose();
|
||||||
|
|
||||||
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = "self" });
|
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = "self" });
|
||||||
|
|
||||||
_ = Task.Run(() => AutoReconnectAsync());
|
_ = Task.Run(() => AutoReconnectAsync());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace EonaCat.Connections
|
||||||
private readonly object _statsLock = new object();
|
private readonly object _statsLock = new object();
|
||||||
|
|
||||||
public event EventHandler<ConnectionEventArgs> OnConnected;
|
public event EventHandler<ConnectionEventArgs> OnConnected;
|
||||||
public event EventHandler<NicknameConnectionEventArgs> OnConnectedWithNickname;
|
public event EventHandler<ConnectionEventArgs> OnConnectedWithNickname;
|
||||||
public event EventHandler<DataReceivedEventArgs> OnDataReceived;
|
public event EventHandler<DataReceivedEventArgs> OnDataReceived;
|
||||||
public event EventHandler<ConnectionEventArgs> OnDisconnected;
|
public event EventHandler<ConnectionEventArgs> OnDisconnected;
|
||||||
public event EventHandler<ErrorEventArgs> OnSslError;
|
public event EventHandler<ErrorEventArgs> OnSslError;
|
||||||
|
@ -346,7 +346,7 @@ namespace EonaCat.Connections
|
||||||
{
|
{
|
||||||
var nickname = stringData.Substring(9);
|
var nickname = stringData.Substring(9);
|
||||||
client.Nickname = nickname;
|
client.Nickname = nickname;
|
||||||
OnConnectedWithNickname?.Invoke(this, new NicknameConnectionEventArgs
|
OnConnectedWithNickname?.Invoke(this, new ConnectionEventArgs
|
||||||
{
|
{
|
||||||
ClientId = client.Id,
|
ClientId = client.Id,
|
||||||
RemoteEndPoint = client.RemoteEndPoint,
|
RemoteEndPoint = client.RemoteEndPoint,
|
||||||
|
@ -365,7 +365,7 @@ namespace EonaCat.Connections
|
||||||
{
|
{
|
||||||
client.Nickname = nickname;
|
client.Nickname = nickname;
|
||||||
}
|
}
|
||||||
OnConnectedWithNickname?.Invoke(this, new NicknameConnectionEventArgs
|
OnConnectedWithNickname?.Invoke(this, new ConnectionEventArgs
|
||||||
{
|
{
|
||||||
ClientId = client.Id,
|
ClientId = client.Id,
|
||||||
RemoteEndPoint = client.RemoteEndPoint,
|
RemoteEndPoint = client.RemoteEndPoint,
|
||||||
|
@ -486,7 +486,7 @@ namespace EonaCat.Connections
|
||||||
Buffer.BlockCopy(lengthPrefix, 0, framed, 0, lengthPrefix.Length);
|
Buffer.BlockCopy(lengthPrefix, 0, framed, 0, lengthPrefix.Length);
|
||||||
Buffer.BlockCopy(data, 0, framed, lengthPrefix.Length, data.Length);
|
Buffer.BlockCopy(data, 0, framed, lengthPrefix.Length, data.Length);
|
||||||
|
|
||||||
data = framed; // replace data with framed payload
|
data = framed; // replace the data with framed payload
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_config.Protocol == ProtocolType.TCP)
|
if (_config.Protocol == ProtocolType.TCP)
|
||||||
|
@ -546,22 +546,25 @@ namespace EonaCat.Connections
|
||||||
|
|
||||||
private async Task DisconnectClientAsync(string clientId)
|
private async Task DisconnectClientAsync(string clientId)
|
||||||
{
|
{
|
||||||
if (_clients.TryRemove(clientId, out var client))
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
if (_clients.TryRemove(clientId, out var client))
|
||||||
{
|
{
|
||||||
client.CancellationToken?.Cancel();
|
try
|
||||||
client.TcpClient?.Close();
|
{
|
||||||
client.Stream?.Dispose();
|
client.CancellationToken?.Cancel();
|
||||||
client.AesEncryption?.Dispose();
|
client.TcpClient?.Close();
|
||||||
|
client.Stream?.Dispose();
|
||||||
|
client.AesEncryption?.Dispose();
|
||||||
|
|
||||||
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = clientId, RemoteEndPoint = client.RemoteEndPoint });
|
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = clientId, RemoteEndPoint = client.RemoteEndPoint, Nickname = client.Nickname });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
OnGeneralError?.Invoke(this, new ErrorEventArgs { ClientId = clientId, Exception = ex, Message = "Error disconnecting client" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
});
|
||||||
{
|
|
||||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { ClientId = clientId, Exception = ex, Message = "Error disconnecting client" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
|
|
50
README.md
50
README.md
|
@ -34,7 +34,6 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
|
|
||||||
## Server example:
|
## Server example:
|
||||||
|
|
||||||
using EonaCat.Connections;
|
|
||||||
using EonaCat.Connections.Models;
|
using EonaCat.Connections.Models;
|
||||||
|
|
||||||
namespace EonaCat.Connections.Server.Example
|
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
|
var config = new Configuration
|
||||||
{
|
{
|
||||||
Protocol = ProtocolType.TCP,
|
Protocol = ProtocolType.TCP,
|
||||||
Port = 8080,
|
Port = 1111,
|
||||||
UseSsl = true,
|
UseSsl = false,
|
||||||
UseAesEncryption = true,
|
UseAesEncryption = true,
|
||||||
MaxConnections = 100000,
|
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);
|
_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) =>
|
_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
|
// Echo back the message
|
||||||
if (e.IsBinary)
|
if (e.IsBinary)
|
||||||
|
@ -103,7 +110,16 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
};
|
};
|
||||||
|
|
||||||
_server.OnDisconnected += (sender, 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();
|
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;
|
||||||
using EonaCat.Connections.Models;
|
using EonaCat.Connections.Models;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace EonaCat.Connections.Client.Example
|
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;
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -131,13 +148,13 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
var message = Console.ReadLine();
|
var message = Console.ReadLine();
|
||||||
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
|
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
_client.DisconnectAsync().ConfigureAwait(false);
|
await _client.DisconnectAsync().ConfigureAwait(false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(message))
|
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,
|
Protocol = ProtocolType.TCP,
|
||||||
Host = "127.0.0.1",
|
Host = "127.0.0.1",
|
||||||
Port = 8080,
|
Port = 1111,
|
||||||
UseSsl = true,
|
UseSsl = false,
|
||||||
UseAesEncryption = true,
|
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);
|
_client = new NetworkClient(config);
|
||||||
|
|
||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
_client.OnConnected += (sender, e) =>
|
_client.OnConnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
||||||
|
};
|
||||||
|
|
||||||
_client.OnDataReceived += (sender, e) =>
|
_client.OnDataReceived += (sender, e) =>
|
||||||
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
|
||||||
_client.OnDisconnected += (sender, e) =>
|
_client.OnDisconnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine("Disconnected from server");
|
Console.WriteLine("Disconnected from server");
|
||||||
|
};
|
||||||
|
|
||||||
await _client.ConnectAsync();
|
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!");
|
await _client.SendAsync("Hello server!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue