mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-27 05:01:02 +00:00
feat(client): reconnecting to the server on disconnect
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { createConnection } from 'node:net';
|
import { Socket } from 'node:net';
|
||||||
import { serialize, deserialize } from 'bson';
|
import { serialize, deserialize } from 'bson';
|
||||||
import EventEmitter from 'node:events';
|
import EventEmitter from 'node:events';
|
||||||
|
|
||||||
@@ -7,15 +7,13 @@ class HelperClient extends EventEmitter {
|
|||||||
super();
|
super();
|
||||||
if (!server?.port) throw new Error('You did not specify the server port.');
|
if (!server?.port) throw new Error('You did not specify the server port.');
|
||||||
this.server = server;
|
this.server = server;
|
||||||
|
this.client = new Socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
this.client = createConnection(
|
this.client.connect(
|
||||||
this.server.port,
|
this.server.port,
|
||||||
this.server.host ? this.server.host : 'localhost',
|
this.server.host ? this.server.host : 'localhost'
|
||||||
() => {
|
|
||||||
this.emit('connect');
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
this.client.on('data', (data) => {
|
this.client.on('data', (data) => {
|
||||||
@@ -24,21 +22,39 @@ class HelperClient extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
switch (eventData.op) {
|
switch (eventData.op) {
|
||||||
case 2: {
|
case 2: {
|
||||||
// The 'aiResponse' event.
|
// The 'aiResponse' event.
|
||||||
|
|
||||||
this.emit('aiResponse', eventData);
|
this.emit('aiResponse', eventData);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 6: {
|
case 6: {
|
||||||
// The 'ocrResponse' event.
|
// The 'ocrResponse' event.
|
||||||
|
|
||||||
this.emit('ocrResponse', eventData);
|
this.emit('ocrResponse', eventData);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tvSdb.on('connect', () => {
|
||||||
|
if (this.reconnectionInterval) {
|
||||||
|
clearInterval(this.reconnectionInterval);
|
||||||
|
this.reconnectionInterval = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit('connect');
|
||||||
|
});
|
||||||
|
|
||||||
|
this.client.on('close', () => {
|
||||||
|
this.reconnectionInterval = setInterval(() => {
|
||||||
|
this.client.connect(
|
||||||
|
this.server.port,
|
||||||
|
this.server.host ? this.server.host : 'localhost'
|
||||||
|
);
|
||||||
|
}, 5000);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sendData(data) {
|
sendData(data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user