Skip to content

Commit

Permalink
feat: create new function to detect disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosouz4dev committed Oct 16, 2020
1 parent 91bcb29 commit 1d70c9c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,8 @@ There are some tricks for a better usage of venom.
#### Keep session alive:

```javascript
// In case of being logged out of whatsapp web
// function to detect conflits and change status
// Force it to keep the current session
// State change
// Detect a logout
// Possible state values:
// CONFLICT
// CONNECTED
Expand All @@ -665,15 +663,19 @@ There are some tricks for a better usage of venom.
// UNLAUNCHED
// UNPAIRED
// UNPAIRED_IDLE

client.onStateChange((state) => {
console.log('State changed: ', state);
const conflits = ['CONFLICT', 'UNPAIRED', 'UNLAUNCHED', 'UNPAIRED_IDLE'];
if (conflits.includes(state)) {
client.useHere();
// Detect a logout
if (state === 'UNPAIRED') console.log('Client logout!');
}
if ('CONFLICT'.includes(state)) client.useHere();
});

// function to detect disconnected, sincronization
// Possible state values:
// DISCONNECTED
// RESUMING
// SYNCING
client.onStreamChange((stream) => {
console.log('Stream changed: ', stream);
if ('DISCONNECTED'.includes(stream)) console.log('logout');
});
```

Expand Down
1 change: 1 addition & 0 deletions src/api/helpers/exposed.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ export enum ExposedFn {
onAck = 'onAck',
onParticipantsChanged = 'onParticipantsChanged',
onStateChange = 'onStateChange',
onStreamChange = 'onStreamChange',
}
23 changes: 23 additions & 0 deletions src/api/layers/listener.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ declare module WAPI {
const waitNewMessages: (rmCallback: boolean, callback: Function) => void;
const allNewMessagesListener: (callback: Function) => void;
const onStateChange: (callback: Function) => void;
const onStreamChange: (callback: Function) => void;
const onAddedToGroup: (callback: Function) => any;
const onParticipantsChanged: (groupId: string, callback: Function) => any;
const onLiveLocation: (chatId: string, callback: Function) => any;
Expand All @@ -72,6 +73,7 @@ declare global {
onMessage: any;
onAnyMessage: any;
onStateChange: any;
onStreamChange: any;
onAck: any;
}
}
Expand Down Expand Up @@ -139,6 +141,27 @@ export class ListenerLayer extends ProfileLayer {
}
}

/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onStreamChange(fn: (state: SocketState) => void) {
var has = await this.page.evaluate(() => {
return window.onStreamChange;
});
if (!has) {
await this.page
.exposeFunction(ExposedFn.onStreamChange, (state: SocketState) =>
fn(state)
)
.then(() =>
this.page.evaluate(() => {
WAPI.onStreamChange((_) => window['onStreamChange'](_.stream));
})
);
}
}

/**
* @event Listens to messages acknowledgement Changes
* @returns Observable stream of messages
Expand Down
21 changes: 13 additions & 8 deletions src/api/model/enum/socket-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,21 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
export enum SocketState {
// for state
CONFLICT = 'CONFLICT',
CONNECTED = 'CONNECTED',
DEPRECATED_VERSION = 'DEPRECATED_VERSION',
OPENING = 'OPENING',
PAIRING = 'PAIRING',
UNPAIRED = 'UNPAIRED',
UNPAIRED_IDLE = 'UNPAIRED_IDLE',
CONNECTED = 'CONNECTED',
TIMEOUT = 'TIMEOUT',
CONFLICT = 'CONFLICT',
UNLAUNCHED = 'UNLAUNCHED',
PROXYBLOCK = 'PROXYBLOCK',
TOS_BLOCK = 'TOS_BLOCK',
SMB_TOS_BLOCK = 'SMB_TOS_BLOCK',
DEPRECATED_VERSION = 'DEPRECATED_VERSION',
TIMEOUT = 'TIMEOUT',
TOS_BLOCK = 'TOS_BLOCK',
UNLAUNCHED = 'UNLAUNCHED',
UNPAIRED = 'UNPAIRED',
UNPAIRED_IDLE = 'UNPAIRED_IDLE',
// for stream
DISCONNECTED = 'DISCONNECTED',
RESUMING = 'RESUMING',
SYNCING = 'SYNCING',
}
2 changes: 0 additions & 2 deletions src/lib/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
declare module WAPI {
const waitNewMessages: (rmCallback: boolean, callback: Function) => void;
const waitNewAcknowledgements: (callback: Function) => void;
const onStateChange: (callback: Function) => void;
const allNewMessagesListener: (callback: Function) => void;
}

enum ExposedFn {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/wapi/listeners/add-on-state-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ export function addOnStateChange() {
window.Store.State.default.on('change:state', callback);
return true;
};
window.WAPI.onStreamChange = function (callback) {
window.Store.State.default.on('change:stream', callback);
return true;
};
}

0 comments on commit 1d70c9c

Please sign in to comment.