forked from emqx/qmqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for mqtt over websockets
This feature will only be compiled if explicitly enabled: qmake CONFIG+=QMQTT_WEBSOCKETS or: qbs build qmqtt.webSocketSupport:true There are 2 reasons: * websocket support relies on the QWebsocket class which is part of the websockets module. Previous versions of qmqtt did not rely on this module, so it may not be present on systems where old versions of the shared library are in use. Adding a new module dependency will make it harder to upgrade this systems. * A call to QWebSocket::open(const QNetworkRequest &) is used, which was introduced in Qt 5.6. qmqtt itself requires version 5.3 or newer.
- Loading branch information
Showing
15 changed files
with
395 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifdef QT_WEBSOCKETS_LIB | ||
|
||
#include <QNetworkRequest> | ||
#include <QUrl> | ||
#include "qmqtt_websocket_p.h" | ||
|
||
QMQTT::WebSocket::WebSocket(const QString& origin, QWebSocketProtocol::Version version, | ||
bool ignoreSelfSigned, QObject* parent) | ||
: SocketInterface(parent) | ||
, _socket(new QWebSocket(origin, version, this)) | ||
, _ioDevice(new WebSocketIODevice(_socket, this)) | ||
, _ignoreSelfSigned(ignoreSelfSigned) | ||
{ | ||
connect(_socket, &QWebSocket::connected, this, &WebSocket::connected); | ||
connect(_socket, &QWebSocket::disconnected, this, &WebSocket::disconnected); | ||
connect(_socket, | ||
static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::error), | ||
this, | ||
static_cast<void (SocketInterface::*)(QAbstractSocket::SocketError)>(&SocketInterface::error)); | ||
connect(_socket, &QWebSocket::sslErrors, this, &WebSocket::sslErrors); | ||
} | ||
|
||
QMQTT::WebSocket::~WebSocket() | ||
{ | ||
} | ||
|
||
void QMQTT::WebSocket::connectToHost(const QHostAddress& address, quint16 port) | ||
{ | ||
Q_UNUSED(address) | ||
Q_UNUSED(port) | ||
qFatal("No supported"); | ||
} | ||
|
||
void QMQTT::WebSocket::connectToHost(const QString& hostName, quint16 port) | ||
{ | ||
Q_UNUSED(port) | ||
QUrl url(hostName); | ||
QNetworkRequest request(url); | ||
request.setRawHeader("Sec-WebSocket-Protocol", "mqtt"); | ||
_ioDevice->connectToHost(request); | ||
} | ||
|
||
void QMQTT::WebSocket::disconnectFromHost() | ||
{ | ||
_socket->disconnect(); | ||
} | ||
|
||
QAbstractSocket::SocketState QMQTT::WebSocket::state() const | ||
{ | ||
return _socket->state(); | ||
} | ||
|
||
QAbstractSocket::SocketError QMQTT::WebSocket::error() const | ||
{ | ||
return _socket->error(); | ||
} | ||
|
||
void QMQTT::WebSocket::sslErrors(const QList<QSslError> &errors) | ||
{ | ||
if (!_ignoreSelfSigned) | ||
return; | ||
foreach (QSslError error, errors) | ||
{ | ||
if (error.error() != QSslError::SelfSignedCertificate && | ||
error.error() != QSslError::SelfSignedCertificateInChain) | ||
{ | ||
return; | ||
} | ||
} | ||
_socket->ignoreSslErrors(); | ||
} | ||
|
||
#endif // QT_WEBSOCKETS_LIB |
Oops, something went wrong.