Verified Commit 8de000d9 authored by Aral Balkan's avatar Aral Balkan
Browse files

Closes #93. Server now closes all open sockets on exit.

parent bf0ed591
......@@ -25,13 +25,22 @@
socket.send('Hello, there!')
}
socket.onclose = event => {
console.log('Socket closed.')
document.querySelector('#message').disabled = true
document.querySelector('button[type="submit"]').disabled = true
document.querySelector('#received').innerHTML += `<li style="color: red;">Socket closed.</li>`
}
socket.onmessage = event => {
document.querySelector('#received').innerHTML += `<li>${event.data}</li>`
}
document.querySelector('#messageForm').addEventListener('submit', event => {
event.preventDefault()
const message = document.querySelector('#message').value
const messageTextInput = document.querySelector('#message')
const message = messageTextInput.value
messageTextInput.value = ''
socket.send(message)
})
......
......@@ -24,6 +24,7 @@ const expressWebSocket = require('express-ws')
const helmet = require('helmet')
const morgan = require('morgan')
const redirectHTTPS = require('redirect-https')
const enableDestroy = require('server-destroy')
const Graceful = require('node-graceful')
const httpProxyMiddleware = require('http-proxy-middleware')
......@@ -354,8 +355,14 @@ class Site {
// Handle graceful exit.
const goodbye = (done) => {
console.log('\n 💃 Preparing to exit gracefully, please wait…')
// Close all active connections on the server.
// (This is so that long-running connections – e.g., WebSockets – do not block the exit.)
server.destroy()
// Stop accepting new connections.
server.close( () => {
// The server close event will be the last one to fire. Let’s say goodbye :)
// OK, it’s time to go :)
console.log('\n 💖 Goodbye!\n')
done()
})
......@@ -377,6 +384,9 @@ class Site {
callback.apply(this, [server])
})
// Enable the ability to destroy the server (close all active connections).
enableDestroy(server)
return server
}
......
......@@ -4707,6 +4707,11 @@
"send": "0.17.1"
}
},
"server-destroy": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/server-destroy/-/server-destroy-1.0.1.tgz",
"integrity": "sha1-8Tv5KOQrnD55OD5hzDmYtdFObN0="
},
"set-blocking": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment