From b67e87d19e80b0f9e8b5c8d4753032ddb08c58ce Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Wed, 15 Nov 2017 21:59:01 -0600 Subject: [PATCH] Attempt to gracefully close the socket --- Ooui/Client.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Ooui/Client.js b/Ooui/Client.js index bff2f13..779ca36 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -18,6 +18,15 @@ const mouseEvents = { wheel: true, }; +window.onbeforeunload = function() { + if (socket != null) { + socket.close (1001, "Unloading page"); + socket = null; + console.log ("Web socket closed"); + } + return null; +} + function ooui (rootElementPath) { var opened = false; @@ -135,7 +144,8 @@ function msgListen (m) { }; } const ems = JSON.stringify (em); - socket.send (ems); + if (socket != null) + socket.send (ems); if (debug) console.log ("Event", em); if (em.k === "submit") e.preventDefault ();