Skip to content

Commit

Permalink
Merge pull request #2454 from mikiher/socket-authority-close
Browse files Browse the repository at this point in the history
Add SocketAuthority.close()
  • Loading branch information
advplyr authored Dec 28, 2023
2 parents 0d0bdce + 9a634e0 commit fbbcedd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class Server {
await this.stop()
Logger.info('Server stopped. Exiting.')
} else {
Logger.info('SIGINT (Ctrl+C) received again. Exiting immediately.')
Logger.info('SIGINT (Ctrl+C) received again. Exiting immediately.')
}
process.exit(0)
})
Expand Down Expand Up @@ -395,13 +395,17 @@ class Server {
res.sendStatus(200)
}

/**
* Gracefully stop server
* Stops watcher and socket server
*/
async stop() {
Logger.info('=== Stopping Server ===')
await this.watcher.close()
Logger.info('Watcher Closed')

return new Promise((resolve) => {
this.server.close((err) => {
SocketAuthority.close((err) => {
if (err) {
Logger.error('Failed to close server', err)
} else {
Expand Down
14 changes: 14 additions & 0 deletions server/SocketAuthority.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ class SocketAuthority {
}
}

/**
* Closes the Socket.IO server and disconnect all clients
*
* @param {Function} callback
*/
close(callback) {
Logger.info('[SocketAuthority] Shutting down')
// This will close all open socket connections, and also close the underlying http server
if (this.io)
this.io.close(callback)
else
callback()
}

initialize(Server) {
this.Server = Server

Expand Down

0 comments on commit fbbcedd

Please sign in to comment.