Skip to content

Commit

Permalink
auto reconnect on unavailable service
Browse files Browse the repository at this point in the history
  • Loading branch information
clairton committed Mar 22, 2024
1 parent 0c4b275 commit 1b141a7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unoapi-cloud",
"version": "1.15.43",
"version": "1.15.44",
"description": "Unoapi Cloud",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/services/auth_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const authState = async (session: session, phone: string) => {
}

const saveCreds: () => Promise<void> = async () => {
logger.debug('save creds %', phone)
logger.debug('save creds %s', phone)
await writeData('', creds)
}

Expand Down
27 changes: 10 additions & 17 deletions src/services/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,32 +163,25 @@ export const connect = async ({
const { lastDisconnect } = payload
const statusCode = lastDisconnect?.error?.output?.statusCode
logger.info(`${phone} disconnected with status: ${statusCode}`)
let reconnectSession = true
if ([DisconnectReason.loggedOut, DisconnectReason.badSession, DisconnectReason.forbidden].includes(statusCode)) {
status.attempt = 1
reconnectSession = false
if (!(await isSessionStatusConnecting(phone))) {
const message = `The session is removed in Whatsapp App, send a message here to reconnect!`
await onNotification(message, true)
}
await logout()
const message = `The session is removed in Whatsapp App, send a message here to reconnect!`
await onNotification(message, true)
await onDisconnected(phone, payload)
return onDisconnected(phone, payload)
} else if (statusCode === DisconnectReason.connectionReplaced) {
reconnectSession = false
await close()
const message = `The session must be unique, close connection, send a message here to reconnect if him was offline!`
return onNotification(message, true)
} else if (statusCode === DisconnectReason.unavailableService) {
await close()
const message = `The service is unavailable, please open the whastapp app to verify and after send a message again!`
return onNotification(message, true)
}
if (reconnectSession) {
if (status.attempt == 1) {
const detail = lastDisconnect?.error?.output?.payload?.error
const message = `The connection is closed with status: ${statusCode}, detail: ${detail}!`
await onNotification(message, true)
}
return reconnect()
if (status.attempt == 1) {
const detail = lastDisconnect?.error?.output?.payload?.error
const message = `The connection is closed with status: ${statusCode}, detail: ${detail}!`
await onNotification(message, true)
}
return reconnect()
}

const getMessage = async (key: proto.IMessageKey): Promise<proto.IMessage | undefined> => {
Expand Down

0 comments on commit 1b141a7

Please sign in to comment.