Example now stops and starts service publish and service discovery when

application is suspended (device sleeps) and becomes active (device wakes).
Important on iOS devices as device sleep kills browser and service
publisher.
This commit is contained in:
Jonathan Bagg
2017-10-09 21:30:17 -04:00
parent 8a9936a4a5
commit c130cb697d
3 changed files with 41 additions and 12 deletions

View File

@@ -107,4 +107,12 @@ qDebug() << zcs->txt["Qt"];
Qt5
On Linux, libavahi-client-dev and libavahi-common-dev
On Linux, libavahi-client-dev and libavahi-common-dev
### Apple App Store deployment
Publishing GPL software in the App Store is a [violation of the GPL](https://news.ycombinator.com/item?id=3488833). If you need to publish an app in the Apple App Store that uses QZeroConf, please contact me for a copy of QZeroConf with a BSD licence.
### iOS device sleep
When iOS puts the device to sleep, it breaks the DNS-SD browser and service publisher. The only way around this is to call stopServicePublish() and stopBrowser() when the application state changes to Qt::ApplicationSuspended (sleep) and then call start startPublish() and startBrowser() when the application state changes to Qt::ApplicationActive (wake). See appStateChanged() in example.

View File

@@ -24,6 +24,7 @@
Example app to demonstrate service publishing and service discovery
---------------------------------------------------------------------------------------------------
**************************************************************************************************/
#include <QGuiApplication>
#include <QVBoxLayout>
#include <QPushButton>
#include <QTableWidgetItem>
@@ -54,9 +55,9 @@ mainWindow::mainWindow()
connect(&zeroConf, SIGNAL(serviceAdded(QZeroConfService *)), this, SLOT(addService(QZeroConfService *)));
connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService *)), this, SLOT(removeService(QZeroConfService *)));
connect(qGuiApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(appStateChanged(Qt::ApplicationState)));
zeroConf.startBrowser("_qtzeroconf_test._tcp");
startPublish();
publishEnabled = 1;
}
void mainWindow::buildGUI()
@@ -68,13 +69,13 @@ void mainWindow::buildGUI()
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(button);
layout->setAlignment(button, Qt::AlignHCenter);
connect(button, SIGNAL(clicked()), this, SLOT(startPublish()));
connect(button, &QPushButton::clicked, this, &mainWindow::startPublishClicked);
button = new QPushButton(tr(" Disable Publish "));
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(button);
layout->setAlignment(button, Qt::AlignHCenter);
connect(button, SIGNAL(clicked()), this, SLOT(stopPublish()));
connect(button, &QPushButton::clicked, this, &mainWindow::stopPublishClicked);
table.verticalHeader()->hide();
table.horizontalHeader()->hide();
@@ -102,21 +103,39 @@ QString mainWindow::buildName(void)
return name;
}
void mainWindow::appStateChanged(Qt::ApplicationState state)
{
if (state == Qt::ApplicationSuspended) {
zeroConf.stopServicePublish();
zeroConf.stopBrowser();
}
else if (state == Qt::ApplicationActive) {
if (publishEnabled && !zeroConf.publishExists())
startPublish();
if (!zeroConf.browserExists())
zeroConf.startBrowser("_qtzeroconf_test._tcp");
}
}
// ---------- Service Publish ----------
void mainWindow::startPublish()
{
if (publishEnabled)
return;
publishEnabled = 1;
zeroConf.clearServiceTxtRecords();
zeroConf.addServiceTxtRecord("Qt", "the best!");
zeroConf.addServiceTxtRecord("ZeroConf is nice too");
zeroConf.startServicePublish(buildName().toUtf8(), "_qtzeroconf_test._tcp", "local", 11437);
}
void mainWindow::stopPublish()
void mainWindow::startPublishClicked()
{
if (publishEnabled)
return;
publishEnabled = 1;
startPublish();
}
void mainWindow::stopPublishClicked()
{
if (!publishEnabled)
return;

View File

@@ -40,14 +40,16 @@ public:
private:
void buildGUI();
void startPublish();
QString buildName(void);
QTableWidget table;
QZeroConf zeroConf;
bool publishEnabled;
private slots:
void startPublish();
void stopPublish();
void appStateChanged(Qt::ApplicationState state);
void startPublishClicked();
void stopPublishClicked();
void addService(QZeroConfService *item);
void removeService(QZeroConfService *item);
};