EonaCat.Connections/EonaCat.Connections.Server/Program.cs

75 lines
2.5 KiB
C#

using EonaCat.Connections;
using EonaCat.Connections.Models;
namespace EonaCat.Connections.Server.Example
{
public class Program
{
private static NetworkServer _server;
public static void Main(string[] args)
{
CreateServerAsync().ConfigureAwait(false);
while (true)
{
Console.Write("Enter message to send (or 'exit' to quit): ");
var message = Console.ReadLine();
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
_server.Stop();
_server.Dispose();
Console.WriteLine("Server stopped.");
break;
}
if (!string.IsNullOrEmpty(message))
{
_server.BroadcastAsync(message).ConfigureAwait(false);
}
}
}
private static async Task CreateServerAsync()
{
var config = new Configuration
{
Protocol = ProtocolType.TCP,
Port = 8080,
UseSsl = true,
UseAesEncryption = true,
MaxConnections = 100000,
ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss"),
};
_server = new NetworkServer(config);
// Subscribe to events
_server.OnConnected += (sender, e) =>
Console.WriteLine($"Client {e.ClientId} connected from {e.RemoteEndPoint}");
_server.OnConnectedWithNickname += (sender, e) =>
Console.WriteLine($"Client {e.ClientId} connected with nickname: {e.Nickname}");
_server.OnDataReceived += async (sender, e) =>
{
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
// Echo back the message
if (e.IsBinary)
{
await _server.SendToClientAsync(e.ClientId, e.Data);
}
else
{
await _server.SendToClientAsync(e.ClientId, $"Echo: {e.StringData}");
}
};
_server.OnDisconnected += (sender, e) =>
Console.WriteLine($"Client {e.ClientId} disconnected");
await _server.StartAsync();
}
}
}