socket.io: Reconnect if the server disconnects

This will make the pages gracefully handle HTTP server restart events,
which happen whenever a plugin is installed or uninstalled via the
`/admin/plugins` page.
This commit is contained in:
Richard Hansen 2020-12-14 02:50:52 -05:00
parent 303964c51e
commit 9f7d42185d
4 changed files with 36 additions and 8 deletions

View file

@ -4,6 +4,11 @@
$(document).ready(() => {
const socket = socketio.connect('..', '/pluginfw/installer');
socket.on('disconnect', (reason) => {
// The socket.io client will automatically try to reconnect for all reasons other than "io
// server disconnect".
if (reason === 'io server disconnect') socket.connect();
});
const search = (searchTerm, limit) => {
if (search.searchTerm !== searchTerm) {
@ -253,10 +258,12 @@ $(document).ready(() => {
search.results = [];
});
// init
updateHandlers();
socket.emit('getInstalled');
search('');
socket.on('connect', () => {
updateHandlers();
socket.emit('getInstalled');
search.searchTerm = null;
search($('#search-query').val());
});
// check for updates every 5mins
setInterval(() => {