Updated
This commit is contained in:
@@ -1,22 +1,30 @@
|
||||
using EonaCat.Connections.Models;
|
||||
using System.Reflection;
|
||||
|
||||
namespace EonaCat.Connections.Server.Example
|
||||
{
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/license for full license details.
|
||||
|
||||
public class Program
|
||||
{
|
||||
private const bool IsHeartBeatEnabled = false;
|
||||
private static NetworkServer _server;
|
||||
|
||||
public static void Main(string[] args)
|
||||
public static bool WaitForMessage { get; private set; } = true;
|
||||
public static bool ToConsoleOnly { get; private set; } = true;
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
CreateServerAsync().ConfigureAwait(false);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.Write("Enter message to send (or 'exit' to quit): ");
|
||||
var message = Console.ReadLine();
|
||||
var message = string.Empty;
|
||||
|
||||
if (WaitForMessage)
|
||||
{
|
||||
Console.Write("Enter message to send (or 'exit' to quit): ");
|
||||
message = Console.ReadLine();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_server.Stop();
|
||||
@@ -27,8 +35,10 @@ namespace EonaCat.Connections.Server.Example
|
||||
|
||||
if (!string.IsNullOrEmpty(message))
|
||||
{
|
||||
_server.BroadcastAsync(message).ConfigureAwait(false);
|
||||
await _server.BroadcastAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Task.Delay(5000).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,33 +47,30 @@ namespace EonaCat.Connections.Server.Example
|
||||
var config = new Configuration
|
||||
{
|
||||
Protocol = ProtocolType.TCP,
|
||||
Host = "0.0.0.0",
|
||||
Port = 1111,
|
||||
UseSsl = true,
|
||||
UseAesEncryption = true,
|
||||
UseSsl = false,
|
||||
UseAesEncryption = false,
|
||||
MaxConnections = 100000,
|
||||
AesPassword = "EonaCat.Connections.Password",
|
||||
Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss")
|
||||
Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss"),
|
||||
EnableHeartbeat = IsHeartBeatEnabled
|
||||
};
|
||||
|
||||
_server = new NetworkServer(config);
|
||||
|
||||
// Subscribe to events
|
||||
_server.OnConnected += (sender, e) =>
|
||||
Console.WriteLine($"Client {e.ClientId} connected from {e.RemoteEndPoint}");
|
||||
{
|
||||
WriteToLog($"Client {e.ClientId} connected from {e.RemoteEndPoint}");
|
||||
Console.WriteLine($"New connection from {e.RemoteEndPoint} with Client ID: {e.ClientId}");
|
||||
};
|
||||
|
||||
_server.OnConnectedWithNickname += (sender, e) =>
|
||||
Console.WriteLine($"Client {e.ClientId} connected with nickname: {e.Nickname}");
|
||||
_server.OnConnectedWithNickname += (sender, e) => WriteToLog($"Client {e.ClientId} connected with nickname: {e.Nickname}");
|
||||
|
||||
_server.OnDataReceived += async (sender, e) =>
|
||||
{
|
||||
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)}");
|
||||
}
|
||||
WriteToLog($"Received from {e.ClientId} ({e.RemoteEndPoint.ToString()}): {(e.IsBinary ? $"{e.Data.Length} bytes" : "a message")}");
|
||||
|
||||
// Echo back the message
|
||||
if (e.IsBinary)
|
||||
@@ -72,23 +79,76 @@ namespace EonaCat.Connections.Server.Example
|
||||
}
|
||||
else
|
||||
{
|
||||
await _server.SendToClientAsync(e.ClientId, $"Echo: {e.StringData}");
|
||||
await _server.SendToClientAsync(e.ClientId, e.StringData);
|
||||
}
|
||||
};
|
||||
|
||||
_server.OnDisconnected += (sender, e) =>
|
||||
{
|
||||
if (e.HasNickname)
|
||||
var message = string.Empty;
|
||||
if (e.Reason == DisconnectReason.LocalClosed)
|
||||
{
|
||||
Console.WriteLine($"Client {e.Nickname} disconnected");
|
||||
if (e.Exception != null)
|
||||
{
|
||||
message = $"{e.Nickname} disconnected from server (local close). Exception: {e.Exception.Message}";
|
||||
}
|
||||
else
|
||||
{
|
||||
message = $"{e.Nickname} disconnected from server (local close).";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Client {e.ClientId} disconnected");
|
||||
if (e.Exception != null)
|
||||
{
|
||||
message = $"{e.Nickname} disconnected from server (remote close). Reason: {e.Reason}. Exception: {e.Exception.Message}";
|
||||
}
|
||||
else
|
||||
{
|
||||
message = $"{e.Nickname} disconnected from server (remote close). Reason: {e.Reason}";
|
||||
}
|
||||
}
|
||||
WriteToLog(message);
|
||||
Console.WriteLine(message);
|
||||
};
|
||||
|
||||
await _server.StartAsync();
|
||||
_ = _server.StartAsync();
|
||||
}
|
||||
|
||||
public static void WriteToLog(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ToConsoleOnly)
|
||||
{
|
||||
var dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
Console.WriteLine($"{dateTimeNow}: {message}");
|
||||
return;
|
||||
}
|
||||
|
||||
var logFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ".", "server_log.txt");
|
||||
var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}{Environment.NewLine}";
|
||||
|
||||
if (!File.Exists(logFilePath))
|
||||
{
|
||||
File.WriteAllText(logFilePath, logMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
if (new FileInfo(logFilePath).Length > 5 * 1024 * 1024) // 5 MB
|
||||
{
|
||||
var archiveFilePath = Path.Combine(Path.GetDirectoryName(logFilePath) ?? ".", $"server_log{DateTime.Now:yyyyMMdd_HHmmss}.txt");
|
||||
File.Move(logFilePath, archiveFilePath);
|
||||
File.WriteAllText(logFilePath, logMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
File.AppendAllText(logFilePath, logMessage);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore logging errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user