@jkt48connect-corp/baileys 7.3.4 → 7.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Socket/socket.js +34 -3
- package/package.json +1 -1
package/lib/Socket/socket.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|
"use strict";
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
4
|
exports.makeSocket = void 0;
|
@@ -19,7 +20,7 @@ const Client_1 = require("./Client");
|
|
19
20
|
*/
|
20
21
|
const makeSocket = (config) => {
|
21
22
|
var _a, _b;
|
22
|
-
const { waWebSocketUrl, connectTimeoutMs, logger, keepAliveIntervalMs, browser, auth: authState, printQRInTerminal, defaultQueryTimeoutMs, transactionOpts, qrTimeout, makeSignalRepository, } = config;
|
23
|
+
const { waWebSocketUrl, connectTimeoutMs, logger, keepAliveIntervalMs, browser, auth: authState, printQRInTerminal, defaultQueryTimeoutMs, transactionOpts, qrTimeout, makeSignalRepository, isAutoReconnect, } = config;
|
23
24
|
let url = typeof waWebSocketUrl === 'string' ? new url_1.URL(waWebSocketUrl) : waWebSocketUrl;
|
24
25
|
config.mobile = config.mobile || url.protocol === 'tcp:';
|
25
26
|
if (config.mobile && url.protocol !== 'tcp:') {
|
@@ -460,7 +461,37 @@ const makeSocket = (config) => {
|
|
460
461
|
}
|
461
462
|
});
|
462
463
|
ws.on('error', mapWebSocketError(end));
|
463
|
-
|
464
|
+
|
465
|
+
// Add auto-reconnect functionality
|
466
|
+
let reconnectAttempts = 0;
|
467
|
+
const maxReconnects = 10;
|
468
|
+
|
469
|
+
ws.on('close', () => {
|
470
|
+
const error = new boom_1.Boom('Connection Terminated', {
|
471
|
+
statusCode: Types_1.DisconnectReason.connectionClosed
|
472
|
+
});
|
473
|
+
|
474
|
+
end(error);
|
475
|
+
|
476
|
+
const shouldReconnect =
|
477
|
+
config.isAutoReconnect &&
|
478
|
+
error.output.statusCode !== Types_1.DisconnectReason.loggedOut &&
|
479
|
+
reconnectAttempts < maxReconnects;
|
480
|
+
|
481
|
+
if (shouldReconnect) {
|
482
|
+
const delay = Math.min(10000, 2000 * Math.pow(2, reconnectAttempts++)); // backoff 2s, 4s, 8s...
|
483
|
+
logger.warn(`Auto reconnect #${reconnectAttempts} in ${delay / 1000}s...`);
|
484
|
+
|
485
|
+
setTimeout(() => {
|
486
|
+
makeSocket({ ...config });
|
487
|
+
}, delay);
|
488
|
+
} else if (!config.isAutoReconnect) {
|
489
|
+
logger.info('Auto reconnect disabled.');
|
490
|
+
} else {
|
491
|
+
logger.info('Auto reconnect stopped: max attempts reached or logged out.');
|
492
|
+
}
|
493
|
+
});
|
494
|
+
|
464
495
|
// the server terminated the connection
|
465
496
|
ws.on('CB:xmlstreamend', () => end(new boom_1.Boom('Connection Terminated by Server', { statusCode: Types_1.DisconnectReason.connectionClosed })));
|
466
497
|
// QR gen
|
@@ -626,4 +657,4 @@ function mapWebSocketError(handler) {
|
|
626
657
|
return (error) => {
|
627
658
|
handler(new boom_1.Boom(`WebSocket Error (${error === null || error === void 0 ? void 0 : error.message})`, { statusCode: (0, Utils_1.getCodeFromWSError)(error), data: error }));
|
628
659
|
};
|
629
|
-
}
|
660
|
+
}
|