From f974ff86ac15f89195bbfcd64ec3f0a1b3641b57 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Fri, 1 May 2020 01:19:26 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=A6=20Fix=20WebSocket=20issues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/client.js | 34 ++++++++++++++-------------------- assets/editor.js | 23 ++++++++++------------- sample/ui.jl | 11 +++++------ src/webserver/WebServer.jl | 20 ++++++++++++++------ 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/assets/client.js b/assets/client.js index fccac71685..166d72074f 100644 --- a/assets/client.js +++ b/assets/client.js @@ -80,17 +80,7 @@ class PlutoConnection { } handleMessage(event) { - - var onFailure = (e) => { - console.warn("Failed to get updates!") - console.log(e) - - this.waitForOnline() - } - - try { - const update = JSON.parse(event.data) - + event.data.text().then((msg) => JSON.parse(msg)).then((update) => { const forMe = !(("notebookID" in update) && (update.notebookID != this.notebookID)) if (!forMe) { console.log("Update message not meant for this notebook") @@ -109,9 +99,12 @@ class PlutoConnection { } this.onUpdate(update, byMe) - } catch (error) { - onFailure(error) - } + }).catch((ex) => { + console.error("Failed to get updates!", ex) + console.log(event) + + this.waitForOnline() + }) } startSocketConnection(onSucces) { @@ -120,8 +113,7 @@ class PlutoConnection { this.handleMessage(e) } this.psocket.onerror = (e) => { - console.error("SOCKET ERROR") - console.log(e) + console.error("SOCKET ERROR", e) if (this.psocket.readyState != WebSocket.OPEN && this.psocket.readyState != WebSocket.CONNECTING) { this.waitForOnline() @@ -139,7 +131,6 @@ class PlutoConnection { console.log(e) this.waitForOnline() - // startSocketConnection(onSucces) } this.psocket.onopen = () => { this.sendreceive("connect", {}).then(u => { @@ -157,7 +148,7 @@ class PlutoConnection { } tryCloseSocketConnection() { - this.psocket.close() + this.psocket.close(1000, "byebye") } initialize(){ @@ -185,14 +176,17 @@ class PlutoConnection { this.clientID = this.getUniqueShortID() this.sentRequests = {} - window.addEventListener("unload", e => { - this.send("disconnect", {}) + window.addEventListener("beforeunload", e => { + //this.send("disconnect", {}) + this.psocket.onclose = undefined + this.tryCloseSocketConnection() }) } fetchPlutoVersions(){ const githubPromise = fetch("https://api.github.com/repos/fonsp/Pluto.jl/releases", { method: 'GET', + mode: 'cors', cache: 'no-cache', headers: { 'Content-Type': 'application/json', diff --git a/assets/editor.js b/assets/editor.js index d93ccd1fa6..8a19b2ffe3 100644 --- a/assets/editor.js +++ b/assets/editor.js @@ -194,25 +194,22 @@ document.addEventListener("DOMContentLoaded", () => { // based on https://stackoverflow.com/a/26716182 // to execute all scripts in the output html: try { - var scripts = Array.prototype.slice.call(outputNode.getElementsByTagName("script")) - for (var i = 0; i < scripts.length; i++) { - if (scripts[i].src != "") { - if (!Array.prototype.map.call(document.head.querySelectorAll("script"), s => s.src).includes(scripts[i])) { - var tag = document.createElement("script") - tag.src = scripts[i].src + Array.from(outputNode.querySelectorAll("script")).map((script) => { + if (script.src != "") { + if (!Array.from(document.head.querySelectorAll("script")).map(s => s.src).includes(script)) { + const tag = document.createElement("script") + tag.src = script.src document.head.appendChild(tag) } } else { - var result = Function(scripts[i].innerHTML).bind(outputNode)() - if (!scripts[i].previousElementSibling && !scripts[i].nextElementSibling) { - if (result && result.nodeType === Node.ELEMENT_NODE) { - scripts[i].parentElement.insertBefore(result, scripts[i]) - } + const result = Function(script.innerHTML).bind(outputNode)() + if (result && result.nodeType === Node.ELEMENT_NODE) { + script.parentElement.insertBefore(result, script) } } - } + }) } catch (err) { - console.error("Couldn't execute all scripts:") + console.error("Couldn't execute script:") console.error(err) // TODO: relay to user // might be wise to wait after adding scripts to head diff --git a/sample/ui.jl b/sample/ui.jl index 51664c35e6..4d9bf3b3c9 100644 --- a/sample/ui.jl +++ b/sample/ui.jl @@ -87,17 +87,18 @@ Try drawing a rectangle in the canvas below 👇 and notice that the `area` vari # ╔═╡ 7f4b0e1e-7f16-11ea-02d3-7955921a70bd @bind dims html""" - +