diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-03-21 16:15:47 +0100 |
---|---|---|
committer | Pierre Rossi <pierre.rossi@gmail.com> | 2014-07-04 12:37:29 +0200 |
commit | 856dc072acb649be03e02ac64c81015d3f2675c2 (patch) | |
tree | 649985d553d6ea92360a66ca291af91c1114596e /src/webchannel/qwebchannel.cpp | |
parent | 9f78e0985d2f4fdc2588be57c5f25afdd59c6365 (diff) | |
download | qtwebchannel-856dc072acb649be03e02ac64c81015d3f2675c2.tar.gz |
Refactor code to use QWebChannelAbstractTransport and QtWebSockets.
This is a quite big changeset, but necessary to get the roadmap
implemented that was discussed at QtCS.
With this patchset landed, the QWebChannel does not depend on
QtWebKit anymore, not even for the tests. Rather, we will introduce
the dependency in the other way (i.e. QtWebKit will optionally use
QtWebChannel if available).
For the pure Qt/C++ use-case, we ship a utility implementation of
a QWebChannelAbstractTransport that uses a QWebSocket for the
server-client communication. This way, we can get rid of the custom
WebSocket implementation.
The tests are refactored to run the qwebchannel.js code directly
inside QML. Integration tests for QtWebKit/QtWebEngine as well
as examples will be added to these repositories.
Change-Id: Icc1c1c5918ec46e31d5070937c14c4ca25a3e2d6
Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'src/webchannel/qwebchannel.cpp')
-rw-r--r-- | src/webchannel/qwebchannel.cpp | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp index 75d1a4b..e4cf0d6 100644 --- a/src/webchannel/qwebchannel.cpp +++ b/src/webchannel/qwebchannel.cpp @@ -43,7 +43,7 @@ #include "qwebchannel.h" #include "qwebchannel_p.h" #include "qmetaobjectpublisher_p.h" -#include "qwebchanneltransportinterface.h" +#include "qwebchannelabstracttransport.h" #include <QJsonDocument> #include <QJsonObject> @@ -64,6 +64,14 @@ QByteArray generateJSONMessage(const QJsonValue &id, const QJsonValue &data, boo return doc.toJson(QJsonDocument::Compact); } +void QWebChannelPrivate::_q_transportDestroyed(QObject *object) +{ + const int idx = transports.indexOf(static_cast<QWebChannelAbstractTransport*>(object)); + if (idx != -1) { + transports.remove(idx); + } +} + QWebChannel::QWebChannel(QObject *parent) : QObject(parent) , d(new QWebChannelPrivate) @@ -75,9 +83,6 @@ QWebChannel::QWebChannel(QObject *parent) QWebChannel::~QWebChannel() { - foreach (QWebChannelTransportInterface *transport, d->transports) { - transport->setMessageHandler(Q_NULLPTR); - } } void QWebChannel::registerObjects(const QHash< QString, QObject * > &objects) @@ -109,20 +114,24 @@ void QWebChannel::setBlockUpdates(bool block) d->publisher->setBlockUpdates(block); } -void QWebChannel::connectTo(QWebChannelTransportInterface *transport) +void QWebChannel::connectTo(QWebChannelAbstractTransport *transport) { Q_ASSERT(transport); if (!d->transports.contains(transport)) { d->transports << transport; - transport->setMessageHandler(d->publisher); + connect(transport, &QWebChannelAbstractTransport::textMessageReceived, + d->publisher, &QMetaObjectPublisher::handleMessage, + Qt::UniqueConnection); + connect(transport, SIGNAL(destroyed(QObject*)), + this, SLOT(_q_transportDestroyed(QObject*))); } } -void QWebChannel::disconnectFrom(QWebChannelTransportInterface *transport) +void QWebChannel::disconnectFrom(QWebChannelAbstractTransport *transport) { const int idx = d->transports.indexOf(transport); if (idx != -1) { - transport->setMessageHandler(Q_NULLPTR); + disconnect(transport, 0, this, 0); d->transports.remove(idx); } } @@ -135,9 +144,12 @@ void QWebChannel::sendMessage(const QJsonValue &id, const QJsonValue &data) cons } const QByteArray &message = generateJSONMessage(id, data, false); - foreach (QWebChannelTransportInterface *transport, d->transports) { - transport->sendMessage(message); + const QString &messageText = QString::fromUtf8(message); + foreach (QWebChannelAbstractTransport *transport, d->transports) { + transport->sendTextMessage(messageText); } } QT_END_NAMESPACE + +#include "moc_qwebchannel.cpp" |