perf: 增加等出 WA 实例

main
LiaoYijun 8 months ago
parent 3ee4230af8
commit 29f29bcf38

@ -49,8 +49,6 @@ const createWhatsApp = async phone => {
const waVersion = version.join('.') + ', ' + (isLatest ? 'latest' : 'out');
const stop = () => {
waSocket.logout(() => '实例已停止');
waEmitter.off('request.' + whatsAppNo + '.send.message', sendMessageHandler);
fs.rm(authStateFolder, { recursive: true, force: true }, (err) => {
if (err) {
@ -67,6 +65,8 @@ const createWhatsApp = async phone => {
console.log('Successfully deleted storeFilename file: ', storeFilename);
}
});
waEmitter.emit('request.' + whatsAppNo + '.stop', {});
};
const start = () => {
@ -310,12 +310,21 @@ const createWhatsApp = async phone => {
});
}
const stopHandler = () => {
waSocket.ev.off('messages.upsert', handleMessagesUpsert);
waSocket.ev.off('messages.update', handleMessagesUpdate);
waSocket.ev.off('creds.update', handleCredsUpdate);
waSocket.logout(() => '实例已停止');
waEmitter.off('request.' + whatsAppNo + '.send.message', sendMessageHandler);
}
waSocket.ev.on('connection.update', async update => {
const { connection, lastDisconnect, qr } = update;
if (connection === 'close') {
waEmitter.off('request.' + whatsAppNo + '.send.message', sendMessageHandler);
waEmitter.off('request.' + whatsAppNo + '.stop', stopHandler);
if((lastDisconnect?.error)?.output?.statusCode !== DisconnectReason.loggedOut) {
start();
} else {
@ -339,6 +348,7 @@ const createWhatsApp = async phone => {
});
waEmitter.on('request.' + whatsAppNo + '.send.message', sendMessageHandler);
waEmitter.on('request.' + whatsAppNo + '.stop', stopHandler);
} else if (qr !== undefined) {
// WebSocket 创建成功等待扫码,如果没有扫码会更新 qr
// 第一次一分钟,后面是 20 秒更新一次

Loading…
Cancel
Save