Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions pkg/whatsmeow/service/whatsmeow.go
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,35 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
mycli.loggerWrapper.GetLogger(instanceID).LogError("[%s] Failed to restart instance: %v", instanceID, err)
}
}(mycli.userID)
case *events.KeepAliveTimeout:
doWebhook = true
postMap["event"] = "KeepAliveTimeout"
postMap["data"] = map[string]interface{}{
"ErrorCount": evt.ErrorCount,
"LastSuccess": evt.LastSuccess,
}
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Keepalive ping timed out (%d consecutive, last success: %s)", mycli.userID, evt.ErrorCount, evt.LastSuccess.Format(time.RFC3339))

// With EnableAutoReconnect disabled, a TCP connection that silently dies
// keeps timing out keepalives forever without ever emitting
// events.Disconnected — the instance stays "connected" but is a zombie.
// whatsmeow's own docs suggest using this event to force a faster
// disconnect+reconnect, so after 3 consecutive timeouts restart the
// instance through the same path the Disconnected handler uses. Exact
// match keeps counts 4, 5, ... from piling up restarts while one is
// already underway.
if evt.ErrorCount == 3 {
go func(instanceID string) {
mycli.loggerWrapper.GetLogger(instanceID).LogWarn("[%s] 3 consecutive keepalive timeouts, restarting instance", instanceID)
if err := mycli.service.ReconnectClient(instanceID); err != nil {
mycli.loggerWrapper.GetLogger(instanceID).LogError("[%s] Failed to restart instance: %v", instanceID, err)
}
}(mycli.userID)
}
case *events.KeepAliveRestored:
doWebhook = true
postMap["event"] = "KeepAliveRestored"
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Keepalive pings restored", mycli.userID)
case *events.LabelEdit:
doWebhook = true
postMap["event"] = "LabelEdit"
Expand Down Expand Up @@ -2222,7 +2251,7 @@ func (w *whatsmeowService) CallWebhook(instance *instance_model.Instance, queueN
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
w.sendToQueueOrWebhook(instance, queueName, jsonData)
}
case "Connected", "PairSuccess", "TemporaryBan", "LoggedOut", "ConnectFailure", "Disconnected":
case "Connected", "PairSuccess", "TemporaryBan", "LoggedOut", "ConnectFailure", "Disconnected", "KeepAliveTimeout", "KeepAliveRestored":
if contains(subscriptions, "CONNECTION") {
w.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Event received of type %s", instance.Id, eventType)
w.sendToQueueOrWebhook(instance, queueName, jsonData)
Expand Down Expand Up @@ -2510,7 +2539,7 @@ func (w *whatsmeowService) SendToGlobalQueues(eventType string, payload []byte,
globalEventType = "CHAT_PRESENCE"
case "CallOffer", "CallAccept", "CallTerminate", "CallOfferNotice", "CallRelayLatency":
globalEventType = "CALL"
case "Connected", "PairSuccess", "TemporaryBan", "LoggedOut", "ConnectFailure", "Disconnected":
case "Connected", "PairSuccess", "TemporaryBan", "LoggedOut", "ConnectFailure", "Disconnected", "KeepAliveTimeout", "KeepAliveRestored":
globalEventType = "CONNECTION"
case "LabelEdit", "LabelAssociationChat", "LabelAssociationMessage":
globalEventType = "LABEL"
Expand Down Expand Up @@ -2572,7 +2601,7 @@ func (w *whatsmeowService) SendToGlobalQueues(eventType string, payload []byte,
globalEventType = "CHAT_PRESENCE"
case "CallOffer", "CallAccept", "CallTerminate", "CallOfferNotice", "CallRelayLatency":
globalEventType = "CALL"
case "Connected", "PairSuccess", "TemporaryBan", "LoggedOut", "ConnectFailure", "Disconnected":
case "Connected", "PairSuccess", "TemporaryBan", "LoggedOut", "ConnectFailure", "Disconnected", "KeepAliveTimeout", "KeepAliveRestored":
globalEventType = "CONNECTION"
case "LabelEdit", "LabelAssociationChat", "LabelAssociationMessage":
globalEventType = "LABEL"
Expand Down