From f05f232d5bb362a00be8078ff7ae424430867a66 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 1 Feb 2018 15:09:37 -0800 Subject: [PATCH] Fix returning to pages without re-requesting them Chrome likes to cache HTML and was making a new web socket request without first requesting a new page. The server however expires a web socket url after its first use. To fix this, web socket urls have a rolling cache policy and can be reconnected to. Fixes #52 --- Ooui.AspNetCore/WebSocketHandler.cs | 31 ++++++++++--------- Ooui/Client.js | 7 +---- .../AspNetCoreMvc/Views/Home/Index.cshtml | 1 + 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/Ooui.AspNetCore/WebSocketHandler.cs b/Ooui.AspNetCore/WebSocketHandler.cs index a483e01..c93d3c4 100644 --- a/Ooui.AspNetCore/WebSocketHandler.cs +++ b/Ooui.AspNetCore/WebSocketHandler.cs @@ -13,19 +13,19 @@ namespace Ooui.AspNetCore public static TimeSpan SessionTimeout { get; set; } = TimeSpan.FromMinutes (5); - static readonly ConcurrentDictionary pendingSessions = - new ConcurrentDictionary (); + static readonly ConcurrentDictionary activeSessions = + new ConcurrentDictionary (); public static string BeginSession (HttpContext context, Element element) { var id = Guid.NewGuid ().ToString ("N"); - var s = new PendingSession { + var s = new ActiveSession { Element = element, - RequestTimeUtc = DateTime.UtcNow, + LastConnectTimeUtc = DateTime.UtcNow, }; - if (!pendingSessions.TryAdd (id, s)) { + if (!activeSessions.TryAdd (id, s)) { throw new Exception ("Failed to schedule pending session"); } @@ -60,20 +60,21 @@ namespace Ooui.AspNetCore } // - // Find the pending session + // Clear old sessions // - if (!pendingSessions.TryRemove (id, out var pendingSession)) { - BadRequest ("Unknown `id`"); - return; + var toClear = activeSessions.Where (x => (DateTime.UtcNow - x.Value.LastConnectTimeUtc) > SessionTimeout).ToList (); + foreach (var c in toClear) { + activeSessions.TryRemove (c.Key, out var _); } // - // Reject the session if it's old + // Find the pending session // - if ((DateTime.UtcNow - pendingSession.RequestTimeUtc) > SessionTimeout) { - BadRequest ("Old `id`"); + if (!activeSessions.TryGetValue (id, out var activeSession)) { + BadRequest ("Unknown `id`"); return; } + activeSession.LastConnectTimeUtc = DateTime.UtcNow; // // Set the element's dimensions @@ -97,14 +98,14 @@ namespace Ooui.AspNetCore // var token = CancellationToken.None; var webSocket = await context.WebSockets.AcceptWebSocketAsync ("ooui"); - var session = new Ooui.UI.Session (webSocket, pendingSession.Element, w, h, token); + var session = new Ooui.UI.Session (webSocket, activeSession.Element, w, h, token); await session.RunAsync ().ConfigureAwait (false); } - class PendingSession + class ActiveSession { public Element Element; - public DateTime RequestTimeUtc; + public DateTime LastConnectTimeUtc; } } } diff --git a/Ooui/Client.js b/Ooui/Client.js index 094aa1a..e053d5c 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -37,7 +37,6 @@ function getSize () { // Main entrypoint function ooui (rootElementPath) { - var opened = false; var initialSize = getSize (); var wsArgs = (rootElementPath.indexOf("?") >= 0 ? "&" : "?") + @@ -47,11 +46,11 @@ function ooui (rootElementPath) { if (location.protocol == "https:") { proto = "wss"; } + socket = new WebSocket (proto + "://" + document.location.host + rootElementPath + wsArgs, "ooui"); socket.addEventListener ("open", function (event) { console.log ("Web socket opened"); - opened = true; }); socket.addEventListener ("error", function (event) { @@ -60,10 +59,6 @@ function ooui (rootElementPath) { socket.addEventListener ("close", function (event) { console.error ("Web socket close", event); - if (opened) { - alert ("Connection to the server has been lost. Please try refreshing the page."); - opened = false; - } }); socket.addEventListener("message", function (event) { diff --git a/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml b/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml index 6fb9f7f..a2cc879 100644 --- a/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml +++ b/PlatformSamples/AspNetCoreMvc/Views/Home/Index.cshtml @@ -9,6 +9,7 @@

Ooui

Write interactive web apps in C# and F#

+

Source Code