QXmppNetworkMonitor Class

The QXmppNetworkMonitor class reconnects the client based on network changes reported by QNetworkInformation. More...

Header: #include <QXmppNetworkMonitor.h>
Since: QXmpp 1.17
Inherits: QXmppClientExtension

Public Functions

bool isActive() const

Detailed Description

Without it, the client only notices a lost connection when a ping times out and only reconnects when its reconnection timer fires. With it:

  • When the network becomes unavailable, the connection is closed immediately (without ending the stream, so it can be resumed) and no reconnection attempts are made until it is available again (see QXmppClient::setNetworkAvailable()).
  • When the network becomes available again, is fully online again or a captive portal has been passed, the client reconnects immediately if it is waiting to reconnect (see QXmppClient::reconnectNow()).
  • When the internet connection is lost while other interfaces keep the network available (reachability drops to QNetworkInformation::Reachability::Local, e.g. Wi-Fi turned off with a VPN interface still up), the connection is closed immediately (without ending the stream) and the client reconnects right away, in case the server is still reachable.
  • When only the connectivity check fails (reachability drops from Online to Site), the connection is checked with a ping, as it may still work.
  • When the transport medium changes (e.g. from Wi-Fi to mobile data), the connection is checked with a ping, as it may have broken without the socket noticing (see QXmppClient::checkConnection()).

Only QNetworkInformation::Reachability::Disconnected is treated as an unavailable network. Other states do not guarantee that the server can (or cannot) be reached, e.g. the server may be in the local network, so they never prevent reconnection attempts.

The monitor is enabled by adding it to the client and disabled by removing it:

QXmppClient client;
client.addNewExtension<QXmppNetworkMonitor>();

If no QNetworkInformation backend has been loaded yet, the monitor loads one that supports reachability when it is added. The backend is process-wide and must be loaded in the thread of the QCoreApplication. If the client lives in another thread, load the backend in the application's thread with QNetworkInformation::loadBackendByFeatures() before adding the monitor. isActive() tells whether a usable backend was found.

Applications that already monitor the network in a different way (e.g. using NetworkManager directly) can call QXmppClient::setNetworkAvailable() and the related functions themselves instead. The two should not be combined.

Member Function Documentation

bool QXmppNetworkMonitor::isActive() const

Returns whether network changes are monitored, i.e. whether the monitor has been added to a client and a QNetworkInformation backend supporting reachability is available.