diff --git a/config/bridge.go b/config/bridge.go index 4168929..6ad096d 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -42,6 +42,7 @@ type BridgeConfig struct { MaxConnectionAttempts int `yaml:"max_connection_attempts"` ConnectionRetryDelay int `yaml:"connection_retry_delay"` ReportConnectionRetry bool `yaml:"report_connection_retry"` + AggressiveReconnect bool `yaml:"aggressive_reconnect"` ChatListWait int `yaml:"chat_list_wait"` PortalSyncWait int `yaml:"portal_sync_wait"` UserMessageBuffer int `yaml:"user_message_buffer"` diff --git a/example-config.yaml b/example-config.yaml index eeb0747..f83b2dc 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -106,6 +106,8 @@ bridge: # Whether or not the bridge should send a notice to the user's management room when it retries connecting. # If false, it will only report when it stops retrying. report_connection_retry: true + # Whether or not the bridge should reconnect even if WhatsApp says another web client connected. + aggressive_reconnect: false # Maximum number of seconds to wait for chats to be sent at startup. # If this is too low and you have lots of chats, it could cause backfilling to fail. chat_list_wait: 30 diff --git a/user.go b/user.go index e828c46..b07b28e 100644 --- a/user.go +++ b/user.go @@ -746,10 +746,14 @@ func (user *User) HandleError(err error) { if closed, ok := err.(*whatsapp.ErrConnectionClosed); ok { user.bridge.Metrics.TrackDisconnection(user.MXID) if closed.Code == 1000 && user.cleanDisconnection { - user.bridge.Metrics.TrackConnectionState(user.JID, false) user.cleanDisconnection = false - user.log.Infoln("Clean disconnection by server") - return + if !user.bridge.Config.Bridge.AggressiveReconnect { + user.bridge.Metrics.TrackConnectionState(user.JID, false) + user.log.Infoln("Clean disconnection by server") + return + } else { + user.log.Debugln("Clean disconnection by server, but aggressive reconnection is enabled") + } } go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection was closed with websocket status code %d", closed.Code)) } else if failed, ok := err.(*whatsapp.ErrConnectionFailed); ok { @@ -1084,8 +1088,8 @@ func (user *User) HandleCommand(cmd whatsappExt.Command) { go portal.UpdateAvatar(user, cmd.ProfilePicInfo, true) } case whatsappExt.CommandDisconnect: - user.cleanDisconnection = true if cmd.Kind == "replaced" { + user.cleanDisconnection = true go user.sendMarkdownBridgeAlert("\u26a0 Your WhatsApp connection was closed by the server because you opened another WhatsApp Web client.\n\n" + "Use the `reconnect` command to disconnect the other client and resume bridging.") } else {