From 3612c855cfc7f2e029c3a966f20bb0458b5f2383 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 15 Apr 2018 17:54:30 -0700 Subject: [PATCH] Log web socket errors --- Ooui.AspNetCore/ElementResult.cs | 8 ++++---- Ooui.AspNetCore/WebSocketHandler.cs | 14 ++++++++------ Ooui/Session.cs | 5 ++++- Ooui/WebAssemblySession.cs | 4 ++-- Ooui/WebSocketSession.cs | 9 +++------ 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Ooui.AspNetCore/ElementResult.cs b/Ooui.AspNetCore/ElementResult.cs index c7f64b6..ab52095 100644 --- a/Ooui.AspNetCore/ElementResult.cs +++ b/Ooui.AspNetCore/ElementResult.cs @@ -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); diff --git a/Ooui.AspNetCore/WebSocketHandler.cs b/Ooui.AspNetCore/WebSocketHandler.cs index 7b479ea..9f88522 100644 --- a/Ooui.AspNetCore/WebSocketHandler.cs +++ b/Ooui.AspNetCore/WebSocketHandler.cs @@ -18,14 +18,15 @@ namespace Ooui.AspNetCore static readonly ConcurrentDictionary pendingSessions = new ConcurrentDictionary (); - 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; } } } diff --git a/Ooui/Session.cs b/Ooui/Session.cs index 07a1b0b..e873370 100644 --- a/Ooui/Session.cs +++ b/Ooui/Session.cs @@ -13,9 +13,12 @@ namespace Ooui protected readonly List queuedMessages = new List (); - 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; diff --git a/Ooui/WebAssemblySession.cs b/Ooui/WebAssemblySession.cs index fbe722d..8f72a39 100644 --- a/Ooui/WebAssemblySession.cs +++ b/Ooui/WebAssemblySession.cs @@ -9,8 +9,8 @@ namespace Ooui readonly string id; readonly Action 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; diff --git a/Ooui/WebSocketSession.cs b/Ooui/WebSocketSession.cs index 5c472df..4ce3ac6 100644 --- a/Ooui/WebSocketSession.cs +++ b/Ooui/WebSocketSession.cs @@ -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 (); }