cleanly handle socket closure
This commit is contained in:
parent
dc1fe1600f
commit
d9501602ec
1 changed files with 15 additions and 1 deletions
16
src/VM.ts
16
src/VM.ts
|
@ -13,6 +13,7 @@ export default class VM {
|
|||
#writeLock : Mutex = new Mutex();
|
||||
#messageQueue : Queue<protocol.ProtocolMessage> = new Queue<protocol.ProtocolMessage>();
|
||||
#nopTimeout : NodeJS.Timeout | null = null;
|
||||
#reconnectTimeout : NodeJS.Timeout | null = null;
|
||||
isConnectedToVM : boolean = false;
|
||||
#noNop : boolean = false;
|
||||
connected : boolean = false;
|
||||
|
@ -29,11 +30,24 @@ export default class VM {
|
|||
this.#socket.connect(socketpath);
|
||||
this.#socket.on('connect', () => {
|
||||
this.connected = true;
|
||||
log("INFO", `Connected to VM at ${socketpath}`);
|
||||
log("INFO", `Connected to ${id} at ${socketpath}`);
|
||||
this.#enqueueMessage({
|
||||
Operation: protocol.ProtocolOperation.NOP
|
||||
});
|
||||
});
|
||||
this.#socket.on('close', () => {
|
||||
this.connected = false;
|
||||
log("INFO", `Disconnected from ${id} at ${socketpath}`);
|
||||
log("INFO", "Reconnecting in 5 seconds...");
|
||||
this.#reconnectTimeout = setTimeout(() => {
|
||||
this.#reconnectTimeout = null;
|
||||
this.#socket.connect(socketpath);
|
||||
}, 5000);
|
||||
});
|
||||
this.#socket.on('error', (err) => {
|
||||
log("ERROR", `Error on socket ${id} at ${socketpath}: ${err.message}`);
|
||||
|
||||
});
|
||||
this.#socket.on('data', (data) => this.#onData(data));
|
||||
this.#events = new EventEmitter();
|
||||
this.#events.on('ack', () => {
|
||||
|
|
Loading…
Reference in a new issue