fix: direct server clients now have unique client IDs

Using the generated security token as an ID was presenting problems with duplicate connections using the same ID and trying to figure out where to send the messages. Now, the clients have a unique ID that's an increasing integer that restarts at 1 when the program restarts.
This commit is contained in:
Andrew Welker
2025-10-15 09:50:12 -05:00
parent 1197b15a33
commit 8525134ae7
10 changed files with 444 additions and 255 deletions

View File

@@ -0,0 +1,97 @@
using PepperDash.Core;
using PepperDash.Core.Logging;
using WebSocketSharp;
namespace PepperDash.Essentials
{
/// <summary>
/// Utility functions for logging and other common tasks.
/// </summary>
public static class Utilities
{
private static int nextClientId = 0;
/// <summary>
/// Get
/// </summary>
/// <returns></returns>
public static int GetNextClientId()
{
nextClientId++;
return nextClientId;
}
/// <summary>
/// Converts a WebSocketServer LogData object to Essentials logging calls.
/// </summary>
/// <param name="data">The LogData object to convert.</param>
/// <param name="message">The log message.</param>
/// <param name="device">The device associated with the log message.</param>
public static void ConvertWebsocketLog(LogData data, string message, IKeyed device = null)
{
switch (data.Level)
{
case LogLevel.Trace:
if (device == null)
{
Debug.LogVerbose(message);
}
else
{
device.LogVerbose(message);
}
break;
case LogLevel.Debug:
if (device == null)
{
Debug.LogDebug(message);
}
else
{
device.LogDebug(message);
}
break;
case LogLevel.Info:
if (device == null)
{
Debug.LogInformation(message);
}
else
{
device.LogInformation(message);
}
break;
case LogLevel.Warn:
if (device == null)
{
Debug.LogWarning(message);
}
else
{
device.LogWarning(message);
}
break;
case LogLevel.Error:
if (device == null)
{
Debug.LogError(message);
}
else
{
device.LogError(message);
}
break;
case LogLevel.Fatal:
if (device == null)
{
Debug.LogFatal(message);
}
else
{
device.LogFatal(message);
}
break;
}
}
}
}