Keep the connectiov alive when not in use

Otherwise, it times out and causes errors
This commit is contained in:
Hippo 2020-06-04 19:10:37 +05:30
parent 01a9b8cf02
commit cfa30a3e73
2 changed files with 30 additions and 2 deletions

View file

@ -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]

View file

@ -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<statusDiv.children.length;i++) {