mirror of
https://github.com/ad1217/PrinterStatus
synced 2024-11-21 23:13:49 -05:00
Automatically reconnect to octoprint instances' websockets
This commit is contained in:
parent
e2cecc27bf
commit
0eedd689b8
@ -98,14 +98,16 @@ class PrinterStatus {
|
||||
passive: 'true',
|
||||
});
|
||||
|
||||
this.connect_websocket(login.name + ':' + login.session);
|
||||
}
|
||||
|
||||
connect_websocket(authToken: string): void {
|
||||
this.websocket = new WebSocket(
|
||||
url.resolve(this.address, '/sockjs/websocket')
|
||||
);
|
||||
this.websocket
|
||||
.on('open', () => {
|
||||
this.websocket!.send(
|
||||
JSON.stringify({ auth: login.name + ':' + login.session })
|
||||
);
|
||||
this.websocket!.send(JSON.stringify({ auth: authToken }));
|
||||
})
|
||||
.on('message', (data: WebSocket.Data) => {
|
||||
const event: octoprint.Message = JSON.parse(data as string);
|
||||
@ -119,6 +121,10 @@ class PrinterStatus {
|
||||
if ('current' in event || 'history' in event) {
|
||||
this.lastStatus = ext_event;
|
||||
}
|
||||
})
|
||||
.on('close', () => {
|
||||
console.log('Lost connection to ' + this.name + ' reconnecting...');
|
||||
setTimeout(() => this.connect_websocket(authToken), 5000);
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user