From 94045057c3d3cfc91bc593a728f36fb72844b078 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 9 Oct 2020 02:16:38 +0000 Subject: [PATCH] Added timeout to upload --- message-creator.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/message-creator.js b/message-creator.js index df276d1..7b4c041 100644 --- a/message-creator.js +++ b/message-creator.js @@ -39,17 +39,34 @@ document.getElementById("createMessageBtn").onclick = async function(){ }, 3000) } -powWorker.addEventListener('message', function(e) { - let decoder = new TextDecoder("utf-8") - let message = decoder.decode(e.data) +async function doUpload(data){ + if (! publicNodes.length){ + setTimeout(function(){ + doUpload(data) + }, 1000) + return + } - fetch('http://' + getCurrentNode() + '.onion/upload', { + let decoder = new TextDecoder("utf-8") + let uploadTimeout = setTimeout(function(){ + console.debug("upload timed out") + doUpload(data) + }, 30000) + let upload = await fetch('http://' + getCurrentNode() + '.onion/upload', { method: 'POST', headers: { "content-type": "application/octet-stream" }, - body: decoder.decode(e.data) + body: decoder.decode(data) }) + clearTimeout(uploadTimeout) + if (upload.ok){ + return + } + doUpload(data) +} +powWorker.addEventListener('message', function(e) { console.debug("Generated block: " + doHashHex(e.data)) + doUpload(e.data) }, false)