2
0
mirror of https://github.com/ad1217/PrinterStatus synced 2024-11-11 02:55:09 -05:00

Automatically reconnect to octoprint instances' websockets

This commit is contained in:
Adam Goldsmith 2020-11-09 12:43:29 -05:00
parent e2cecc27bf
commit 0eedd689b8

View File

@ -98,14 +98,16 @@ class PrinterStatus {
passive: 'true', passive: 'true',
}); });
this.connect_websocket(login.name + ':' + login.session);
}
connect_websocket(authToken: string): void {
this.websocket = new WebSocket( this.websocket = new WebSocket(
url.resolve(this.address, '/sockjs/websocket') url.resolve(this.address, '/sockjs/websocket')
); );
this.websocket this.websocket
.on('open', () => { .on('open', () => {
this.websocket!.send( this.websocket!.send(JSON.stringify({ auth: authToken }));
JSON.stringify({ auth: login.name + ':' + login.session })
);
}) })
.on('message', (data: WebSocket.Data) => { .on('message', (data: WebSocket.Data) => {
const event: octoprint.Message = JSON.parse(data as string); const event: octoprint.Message = JSON.parse(data as string);
@ -119,6 +121,10 @@ class PrinterStatus {
if ('current' in event || 'history' in event) { if ('current' in event || 'history' in event) {
this.lastStatus = ext_event; this.lastStatus = ext_event;
} }
})
.on('close', () => {
console.log('Lost connection to ' + this.name + ' reconnecting...');
setTimeout(() => this.connect_websocket(authToken), 5000);
}); });
} }