Log web socket errors

This commit is contained in:
Frank A. Krueger 2018-04-15 17:54:30 -07:00
parent f9a0fd732a
commit 3612c855cf
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
5 changed files with 21 additions and 19 deletions

View File

@ -11,15 +11,15 @@ namespace Ooui.AspNetCore
{
readonly Element element;
readonly string title;
readonly bool disposeWhenDone;
readonly bool disposeAfterSession;
readonly ILogger logger;
public ElementResult (Element element, string title = "", bool disposeWhenDone = true, ILogger logger = null)
public ElementResult (Element element, string title = "", bool disposeAfterSession = true, ILogger logger = null)
{
this.logger = logger;
this.element = element;
this.title = title;
this.disposeWhenDone = disposeWhenDone;
this.disposeAfterSession = disposeAfterSession;
}
public override async Task ExecuteResultAsync (ActionContext context)
@ -34,7 +34,7 @@ namespace Ooui.AspNetCore
element.Style.Height = GetCookieDouble (context.HttpContext.Request.Cookies, "oouiWindowHeight", 24, 480, 10000);
}
var sessionId = WebSocketHandler.BeginSession (context.HttpContext, element, disposeWhenDone);
var sessionId = WebSocketHandler.BeginSession (context.HttpContext, element, disposeAfterSession, logger);
var initialHtml = element.OuterHtml;
var html = UI.RenderTemplate (WebSocketHandler.WebSocketPath + "?id=" + sessionId, title: title, initialHtml: initialHtml);
var htmlBytes = Encoding.UTF8.GetBytes (html);

View File

@ -18,14 +18,15 @@ namespace Ooui.AspNetCore
static readonly ConcurrentDictionary<string, PendingSession> pendingSessions =
new ConcurrentDictionary<string, PendingSession> ();
public static string BeginSession (HttpContext context, Element element, bool disposeElementWhenDone)
public static string BeginSession (HttpContext context, Element element, bool disposeElementAfterSession, ILogger logger)
{
var id = Guid.NewGuid ().ToString ("N");
var s = new PendingSession {
Element = element,
CreateTimeUtc = DateTime.UtcNow,
DisposeElementWhenDone = disposeElementWhenDone,
DisposeElementAfterSession = disposeElementAfterSession,
Logger = logger,
};
if (!pendingSessions.TryAdd (id, s)) {
@ -35,7 +36,7 @@ namespace Ooui.AspNetCore
return id;
}
public static async Task HandleWebSocketRequestAsync (HttpContext context, ILogger logger = null)
public static async Task HandleWebSocketRequestAsync (HttpContext context)
{
void BadRequest (string message)
{
@ -104,7 +105,7 @@ namespace Ooui.AspNetCore
//
try {
webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui").ConfigureAwait (false);
var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementWhenDone, w, h, token);
var session = new Ooui.WebSocketSession (webSocket, activeSession.Element, activeSession.DisposeElementAfterSession, w, h, token);
await session.RunAsync ().ConfigureAwait (false);
}
catch (System.Net.WebSockets.WebSocketException ex) when (ex.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely) {
@ -112,7 +113,7 @@ namespace Ooui.AspNetCore
}
catch (Exception ex) {
context.Abort ();
Console.WriteLine (ex);
activeSession?.Logger?.LogWarning (ex, "Web socket session failed");
}
finally {
webSocket?.Dispose ();
@ -123,7 +124,8 @@ namespace Ooui.AspNetCore
{
public Element Element;
public DateTime CreateTimeUtc;
public bool DisposeElementWhenDone;
public bool DisposeElementAfterSession;
public ILogger Logger;
}
}
}

View File

@ -13,9 +13,12 @@ namespace Ooui
protected readonly List<Message> queuedMessages = new List<Message> ();
public Session (Element element, double initialWidth, double initialHeight)
protected readonly bool disposeElementAfterSession;
public Session (Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight)
{
this.element = element;
this.disposeElementAfterSession = disposeElementAfterSession;
this.initialWidth = initialWidth;
this.initialHeight = initialHeight;

View File

@ -9,8 +9,8 @@ namespace Ooui
readonly string id;
readonly Action<Message> handleElementMessageSent;
public WebAssemblySession (string id, Element element, bool disposeElementWhenDone, double initialWidth, double initialHeight)
: base (element, initialWidth, initialHeight)
public WebAssemblySession (string id, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight)
: base (element, disposeElementAfterSession, initialWidth, initialHeight)
{
this.id = id;
handleElementMessageSent = QueueMessage;

View File

@ -23,13 +23,10 @@ namespace Ooui
DateTime lastTransmitTime = DateTime.MinValue;
readonly TimeSpan throttleInterval = TimeSpan.FromSeconds (1.0 / UI.MaxFps);
readonly bool disposeElementWhenDone;
public WebSocketSession (WebSocket webSocket, Element element, bool disposeElementWhenDone, double initialWidth, double initialHeight, CancellationToken serverToken)
: base (element, initialWidth, initialHeight)
public WebSocketSession (WebSocket webSocket, Element element, bool disposeElementAfterSession, double initialWidth, double initialHeight, CancellationToken serverToken)
: base (element, disposeElementAfterSession, initialWidth, initialHeight)
{
this.webSocket = webSocket;
this.disposeElementWhenDone = disposeElementWhenDone;
//
// Create a new session cancellation token that will trigger
@ -116,7 +113,7 @@ namespace Ooui
finally {
element.MessageSent -= handleElementMessageSent;
if (disposeElementWhenDone && (element is IDisposable disposable)) {
if (disposeElementAfterSession && (element is IDisposable disposable)) {
try {
disposable.Dispose ();
}