From cfa30a3e73ea3f13042a1061918b62f82a7f4b31 Mon Sep 17 00:00:00 2001 From: Hippo Date: Thu, 4 Jun 2020 19:10:37 +0530 Subject: [PATCH] Keep the connectiov alive when not in use Otherwise, it times out and causes errors --- server.js | 6 ++++++ static/app.js | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index a40c29a..dfb654d 100644 --- a/server.js +++ b/server.js @@ -185,6 +185,12 @@ app.ws('/ws/fetch-medium', (ws, req) => { app.ws('/ws/push-ghost', (ws, req) => { ws.on('message', async(msg) => { + // respond to keepalive + if (msg == '__ping__') { + ws.send('__pong__') + return + } + command = msg.split(' ') if (command.length == 3 && command[0] == 'push') { const postSlug = command[1] diff --git a/static/app.js b/static/app.js index ebb933a..26ecfa7 100644 --- a/static/app.js +++ b/static/app.js @@ -6,6 +6,8 @@ function addNotification(msg, className='is-warning') { document.getElementById('notifications').appendChild(n) } +// Fetching from Medium + function mediumFetch(e) { if(e) { e.preventDefault() } @@ -32,12 +34,20 @@ function mediumFetch(e) { socket.onopen = () => { socket.send('fetch ' + postSlug + ' ' + password) + + // keepAlive + socket.send('__ping__') } socket.onmessage = m => { console.log('Got message: ' + m.data) - console.log(m.data) + // process keepAlive + if (m.data == '__pong__') { + setTimeout(()=>{socket.send('__ping__')}, 2000) + return + } + if (m.data.indexOf(': ') != -1) { var command = m.data.slice(0, m.data.indexOf(': ')) var commandData = m.data.slice(m.data.indexOf(': ')+2) @@ -59,6 +69,7 @@ function mediumFetch(e) { // since it's done, clean up // remove statusbar statusDiv.removeChild(statusText) + socket.close() // show buttons, change text and save slug @@ -79,6 +90,9 @@ function mediumFetch(e) { } } + +// Pushing to Ghost + function ghostPush(e) { if(e) { e.preventDefault() } @@ -104,12 +118,19 @@ function ghostPush(e) { socket.onopen = () => { socket.send('push ' + postSlug + ' ' + password) + + // keepAlive + socket.send('__ping__') } socket.onmessage = m => { console.log('Got message: ' + m.data) - console.log(m.data) + // process keepAlive + if (m.data == '__pong__') { + setTimeout(()=>{socket.send('__ping__')}, 2000) + return + } if (m.data.indexOf(': ') != -1) { var command = m.data.slice(0, m.data.indexOf(': ')) @@ -132,6 +153,7 @@ function ghostPush(e) { // since it's done, clean up // remove statusbar statusDiv.removeChild(statusText) + socket.close() // show buttons and change text for (i=0;i