mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-19 03:59:16 +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
|
||||
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.exposeFunction('onAddMessageEvent', (msg) => {
|
||||
await page.exposeFunction('onAddMessageEvent', msg => {
|
||||
if (msg.id.fromMe) return;
|
||||
|
||||
this.emit('message', new Message(this, msg));
|
||||
this.emit(Events.MESSAGE_CREATE, new Message(this, msg));
|
||||
});
|
||||
|
||||
await page.exposeFunction('onConnectionChangedEvent', (conn, connected) => {
|
||||
if (!connected) {
|
||||
this.emit(Events.DISCONNECTED);
|
||||
this.destroy();
|
||||
}
|
||||
})
|
||||
|
||||
await page.evaluate(() => {
|
||||
Store.Msg.on('add', onAddMessageEvent);
|
||||
Store.Conn.on('change:connected', onConnectionChangedEvent);
|
||||
})
|
||||
|
||||
this.pupBrowser = browser;
|
||||
@@ -78,7 +85,7 @@ class Client extends EventEmitter {
|
||||
let chat = await this.pupPage.evaluate(chatId => {
|
||||
return Store.Chat.get(chatId).serialize();
|
||||
}, chatId);
|
||||
|
||||
|
||||
return new Chat(this, chat);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ exports.Events = {
|
||||
AUTHENTICATED: 'authenticated',
|
||||
READY: 'ready',
|
||||
MESSAGE_CREATE: 'message',
|
||||
QR_RECEIVED: 'qr'
|
||||
QR_RECEIVED: 'qr',
|
||||
DISCONNECTED: 'disconnected'
|
||||
}
|
||||
|
||||
exports.MessageTypes = {
|
||||
|
||||
@@ -53,7 +53,6 @@ exports.ExposeStore = () => {
|
||||
* Adds extra props to the serialization of a model
|
||||
*/
|
||||
exports.LoadExtraProps = (model, props) => {
|
||||
console.log(model, props);
|
||||
Store[model].models[0].__props = Store[model].models[0].__props.concat(props);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user