mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 20:49:14 +00:00
[ADD] Disconnected event
This commit is contained in:
@@ -27,5 +27,9 @@ client.on('message', (msg) => {
|
|||||||
// Send a new message to the same chat
|
// Send a new message to the same chat
|
||||||
client.sendMessage(msg.from, 'pong');
|
client.sendMessage(msg.from, 'pong');
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('disconnected', () => {
|
||||||
|
console.log('Client was logged out');
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -48,14 +48,21 @@ class Client extends EventEmitter {
|
|||||||
await page.evaluate(LoadExtraProps, model.WAppModel, model.extraFields);
|
await page.evaluate(LoadExtraProps, model.WAppModel, model.extraFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
await page.exposeFunction('onAddMessageEvent', (msg) => {
|
await page.exposeFunction('onAddMessageEvent', msg => {
|
||||||
if (msg.id.fromMe) return;
|
if (msg.id.fromMe) return;
|
||||||
|
this.emit(Events.MESSAGE_CREATE, new Message(this, msg));
|
||||||
this.emit('message', new Message(this, msg));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await page.exposeFunction('onConnectionChangedEvent', (conn, connected) => {
|
||||||
|
if (!connected) {
|
||||||
|
this.emit(Events.DISCONNECTED);
|
||||||
|
this.destroy();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
Store.Msg.on('add', onAddMessageEvent);
|
Store.Msg.on('add', onAddMessageEvent);
|
||||||
|
Store.Conn.on('change:connected', onConnectionChangedEvent);
|
||||||
})
|
})
|
||||||
|
|
||||||
this.pupBrowser = browser;
|
this.pupBrowser = browser;
|
||||||
@@ -78,7 +85,7 @@ class Client extends EventEmitter {
|
|||||||
let chat = await this.pupPage.evaluate(chatId => {
|
let chat = await this.pupPage.evaluate(chatId => {
|
||||||
return Store.Chat.get(chatId).serialize();
|
return Store.Chat.get(chatId).serialize();
|
||||||
}, chatId);
|
}, chatId);
|
||||||
|
|
||||||
return new Chat(this, chat);
|
return new Chat(this, chat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ exports.Events = {
|
|||||||
AUTHENTICATED: 'authenticated',
|
AUTHENTICATED: 'authenticated',
|
||||||
READY: 'ready',
|
READY: 'ready',
|
||||||
MESSAGE_CREATE: 'message',
|
MESSAGE_CREATE: 'message',
|
||||||
QR_RECEIVED: 'qr'
|
QR_RECEIVED: 'qr',
|
||||||
|
DISCONNECTED: 'disconnected'
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.MessageTypes = {
|
exports.MessageTypes = {
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ exports.ExposeStore = () => {
|
|||||||
* Adds extra props to the serialization of a model
|
* Adds extra props to the serialization of a model
|
||||||
*/
|
*/
|
||||||
exports.LoadExtraProps = (model, props) => {
|
exports.LoadExtraProps = (model, props) => {
|
||||||
console.log(model, props);
|
|
||||||
Store[model].models[0].__props = Store[model].models[0].__props.concat(props);
|
Store[model].models[0].__props = Store[model].models[0].__props.concat(props);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user