Files
QtZeroConf/README.md
T

145 lines
5.0 KiB
Markdown
Raw Normal View History

2015-11-21 07:22:36 -05:00
2015-11-21 08:07:47 -05:00
QZeroConf is a Qt wrapper class for ZeroConf libraries across various platforms.
2015-11-21 07:22:36 -05:00
2015-11-21 08:07:47 -05:00
* Windows (requires iTunes or [Apple Print Services](https://support.apple.com/kb/DL999))
2015-11-21 07:22:36 -05:00
* Mac
* Linux
* Android
* iOS
QZeroConf wraps avahi-client on Linux, avahi-core on Android, and dnssd on Mac, iOS and Windows.
### Building
QZeroConf can be built directly into your project if your project is [LGPL3](http://www.gnu.org/licenses/lgpl-3.0.en.html) compatible. If your project is closed source, you can build QZeroConf as a dynamic library and link against it.
#### Building into your project
1. Clone or download QZeroConf. If you download, unzip.
2. Copy the qtzeroconf directory to be under your project's directory.
3. Include the qtzeroconf.pri file in your projects .pro file
include(qtzeroconf/qtzeroconf.pri)
4. Add QZEROCONF_STATIC define in your projects .pro file
DEFINES= QZEROCONF_STATIC
2015-11-21 07:22:36 -05:00
#### Compiling as a dynamic library
1. Clone or download QZeroConf. If you download, unzip.
2. Enter the qtzeroconf directory, run qmake and then make.
2020-09-10 16:05:36 +02:00
#### Building with CMake
Use `BUILD_SHARED_LIBS` to control whether QZeroConf should be built as static (`-DBUILD_SHARED_LIBS=OFF`) or as shared (`-DBUILD_SHARED_LIBS=ON`) library.
The default is `OFF`.
2020-09-11 09:34:23 +02:00
You can also build the included example project by setting `BUILD_EXAMPLE` to `ON`.
The default for this is `OFF`
2015-11-21 07:22:36 -05:00
### API
#### Service Publishing
(See the example included with the source)
2017-03-10 20:36:25 -05:00
1) Include header
2015-11-21 07:22:36 -05:00
```c++
#include "qzeroconf.h"
```
2017-03-10 20:36:25 -05:00
2) Create an instance of QZeroConf
2015-11-21 07:22:36 -05:00
```c++
QZeroConf zeroConf;
```
It is recommend, but not required, that you connect a slot to QZeroConf's error() signal and servicePublished() signal.
2017-03-10 21:01:23 -05:00
3) If you want to add one or more txt records to the service, call
```c++
zeroConf.addServiceTxtRecord("name", "value");
```
or
```c++
zeroConf.addServiceTxtRecord("nameOnly");
```
before calling startServicePublish()
4) Call startServicePublish() with the name, type, domain and port of your service.
2015-11-21 07:22:36 -05:00
```c++
zeroConf.startServicePublish("Test", "_test._tcp", "local", 12345);
```
QZeroConf will emit servicePublished() if successful, or the error() signal if registration fails.
Service publishing can be stopped by calling stopServicePublish().
Only one service can be published per instance of QZeroConf.
#### Service Discovery
(See the example included with the source)
2017-03-10 20:36:25 -05:00
1) Include header
2015-11-21 07:22:36 -05:00
```c++
#include "qzeroconf.h"
```
2017-03-10 20:36:25 -05:00
2) Create an instance of QZeroConf
2015-11-21 07:22:36 -05:00
```c++
QZeroConf zeroConf;
```
It is recommend, but not required, that you connect a slot to QZeroConf's error() signal.
2018-04-15 12:33:50 -04:00
3) Connect a slot to QZeroConf's serviceAdded() signal. When serviceAdded() is emitted, it passes the QZeroConfService recently discovered. QZeroConfServices are [shared objects](http://doc.qt.io/qt-5/implicit-sharing.html). They are safe to use between threads.
2015-11-21 07:22:36 -05:00
2018-04-15 12:33:50 -04:00
4) Optionally connect a slot to QZeroConf's serviceRemoved() signal to received status when the service is unpublished. ServiceRemoved() passes the QZeroConfService being removed.
2015-11-21 07:22:36 -05:00
2017-03-10 20:36:25 -05:00
5) Call startBrowser() with the type of the service to browse for and optionally the protocol to use.
2015-11-21 07:22:36 -05:00
```c++
startBrowser("_test._tcp");
```
2015-11-21 08:07:47 -05:00
If you are browsing for services published using both ipv4 and ipv6 ( QAbstractSocket::AnyIPProtocol) you should also connect a slot to QzeroConf's serviceUpdated() signal. When the IP address of the first protocol is resolved, serviceAdded() is emitted, when the IP address of the second protocol is resolved, serviceUpdated() is emitted.
2015-11-21 07:22:36 -05:00
Only one browser can be in use per instance of QzeroConf.
2017-03-15 20:57:50 -04:00
**Txt records** are placed into a QMap called txt within the discovered service. For example, the value of txt record "Qt=The Best!" can be retrieved with the code...
2017-03-15 20:32:28 -04:00
```c++
2017-03-15 20:57:50 -04:00
qDebug() << zcs->txt["Qt"];
2017-03-15 20:32:28 -04:00
```
2018-04-15 12:33:50 -04:00
**QML**
QZeroConf can be used in QML applications
2017-03-15 20:32:28 -04:00
2015-11-21 07:22:36 -05:00
### Build Dependencies
Qt5
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 startPublish() and startBrowser() when the application state changes to Qt::ApplicationActive (wake). See appStateChanged() in example.
2021-09-17 20:17:18 -04:00
### iOS 14 and up
iOS 14 and up requires apps to have permissions to access the local network. See [this video](https://developer.apple.com/videos/play/wwdc2020/10110/) Two keys must be added to the info.plist....
```xml
1. <key>NSLocalNetworkUsageDescription</key>
<string>This app will need access to the local network for Discovery services.</string>
2. <key>NSBonjourServices</key>
<array>
<string>_myservice1._tcp</string>
<string>_myservice2._tcp</string>
</array>
```