fix: log errors and disconnects for UI Clients

This commit is contained in:
Andrew Welker
2025-09-25 08:36:24 -05:00
parent 06341b14f3
commit fd70377c7f
8 changed files with 253 additions and 240 deletions

View File

@@ -1,23 +1,27 @@
using Newtonsoft.Json;
using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.AppServer.Messengers;
using PepperDash.Essentials.RoomBridges;
using Serilog.Events;
using System;
using System.Text.RegularExpressions;
using WebSocketSharp;
using WebSocketSharp.Server;
using ErrorEventArgs = WebSocketSharp.ErrorEventArgs;
namespace PepperDash.Essentials.WebSocketServer
{
{
/// <summary>
/// Represents the behaviour to associate with a UiClient for WebSocket communication
/// </summary>
public class UiClient : WebSocketBehavior
public class UiClient : WebSocketBehavior, IKeyed
{
/// <inheritdoc />
public string Key { get; private set; }
public MobileControlSystemController Controller { get; set; }
public string RoomKey { get; set; }
@@ -41,17 +45,18 @@ namespace PepperDash.Essentials.WebSocketServer
}
}
public UiClient()
public UiClient(string key)
{
Key = key;
}
/// <inheritdoc />
protected override void OnOpen()
{
base.OnOpen();
var url = Context.WebSocket.Url;
Debug.LogMessage(LogEventLevel.Verbose, "New WebSocket Connection from: {0}", null, url);
this.LogInformation("New WebSocket Connection from: {url}", url);
var match = Regex.Match(url.AbsoluteUri, "(?:ws|wss):\\/\\/.*(?:\\/mc\\/api\\/ui\\/join\\/)(.*)");
@@ -117,6 +122,7 @@ namespace PepperDash.Essentials.WebSocketServer
Controller.SendMessageObjectToDirectClient(message);
}
/// <inheritdoc />
protected override void OnMessage(MessageEventArgs e)
{
base.OnMessage(e);
@@ -128,18 +134,21 @@ namespace PepperDash.Essentials.WebSocketServer
}
}
/// <inheritdoc />
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
Debug.LogMessage(LogEventLevel.Verbose, "WebSocket UiClient Closing: {0} reason: {1}", null, e.Code, e.Reason);
this.LogInformation("WebSocket UiClient Closing: {code} reason: {reason}", e.Code, e.Reason);
}
/// <inheritdoc />
protected override void OnError(ErrorEventArgs e)
{
base.OnError(e);
Debug.LogMessage(LogEventLevel.Verbose, "WebSocket UiClient Error: {exception} message: {message}", e.Exception, e.Message);
this.LogError("WebSocket UiClient Error: {message}", e.Message);
this.LogDebug(e.Exception, "Stack Trace");
}
}
}