Skip to content

Commit c24d476

Browse files
committed
fix: fecha socket anterior antes de recriar cliente Baileys
QR Code as vezes era gerado mas o WhatsApp respondia com "Nao foi possivel conectar o dispositivo". Causa raiz: createClient sobrescrevia this.client com um novo makeWASocket(...) sem fechar o socket anterior, deixando handlers de evento antigos vivos e disputando a mesma authState/credenciais com o socket novo - corrompendo o handshake da nova sessao. Antes de criar o socket novo, fecha explicitamente o anterior (ws.close() + end()), reaproveitando o mesmo padrao ja usado no handler de connection.update para fechamento de conexao ('close').
1 parent fa09d37 commit c24d476

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,15 @@ export class BaileysStartupService extends ChannelStartupService {
695695

696696
this.endSession = false;
697697

698+
if (this.client) {
699+
try {
700+
this.client.ws?.close();
701+
this.client.end(new Error('Recreating socket connection'));
702+
} catch (error) {
703+
this.logger.warn(`Error closing previous socket before recreating client: ${error}`);
704+
}
705+
}
706+
698707
this.client = makeWASocket(socketConfig);
699708

700709
if (this.localSettings.wavoipToken && this.localSettings.wavoipToken.length > 0) {

0 commit comments

Comments
 (0)