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) => { app.ws('/ws/push-ghost', (ws, req) => {
ws.on('message', async(msg) => { ws.on('message', async(msg) => {
// respond to keepalive
if (msg == '__ping__') {
ws.send('__pong__')
return
}
command = msg.split(' ') command = msg.split(' ')
if (command.length == 3 && command[0] == 'push') { if (command.length == 3 && command[0] == 'push') {
const postSlug = command[1] const postSlug = command[1]

View file

@ -6,6 +6,8 @@ function addNotification(msg, className='is-warning') {
document.getElementById('notifications').appendChild(n) document.getElementById('notifications').appendChild(n)
} }
// Fetching from Medium
function mediumFetch(e) { function mediumFetch(e) {
if(e) { e.preventDefault() } if(e) { e.preventDefault() }
@ -32,12 +34,20 @@ function mediumFetch(e) {
socket.onopen = () => { socket.onopen = () => {
socket.send('fetch ' + postSlug + ' ' + password) socket.send('fetch ' + postSlug + ' ' + password)
// keepAlive
socket.send('__ping__')
} }
socket.onmessage = m => { socket.onmessage = m => {
console.log('Got message: ' + m.data) 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) { if (m.data.indexOf(': ') != -1) {
var command = m.data.slice(0, m.data.indexOf(': ')) var command = m.data.slice(0, m.data.indexOf(': '))
var commandData = m.data.slice(m.data.indexOf(': ')+2) var commandData = m.data.slice(m.data.indexOf(': ')+2)
@ -59,6 +69,7 @@ function mediumFetch(e) {
// since it's done, clean up // since it's done, clean up
// remove statusbar // remove statusbar
statusDiv.removeChild(statusText) statusDiv.removeChild(statusText)
socket.close()
// show buttons, change text and save slug // show buttons, change text and save slug
@ -79,6 +90,9 @@ function mediumFetch(e) {
} }
} }
// Pushing to Ghost
function ghostPush(e) { function ghostPush(e) {
if(e) { e.preventDefault() } if(e) { e.preventDefault() }
@ -104,12 +118,19 @@ function ghostPush(e) {
socket.onopen = () => { socket.onopen = () => {
socket.send('push ' + postSlug + ' ' + password) socket.send('push ' + postSlug + ' ' + password)
// keepAlive
socket.send('__ping__')
} }
socket.onmessage = m => { socket.onmessage = m => {
console.log('Got message: ' + m.data) 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) { if (m.data.indexOf(': ') != -1) {
var command = m.data.slice(0, m.data.indexOf(': ')) var command = m.data.slice(0, m.data.indexOf(': '))
@ -132,6 +153,7 @@ function ghostPush(e) {
// since it's done, clean up // since it's done, clean up
// remove statusbar // remove statusbar
statusDiv.removeChild(statusText) statusDiv.removeChild(statusText)
socket.close()
// show buttons and change text // show buttons and change text
for (i=0;i<statusDiv.children.length;i++) { for (i=0;i<statusDiv.children.length;i++) {