diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2013-12-19 14:26:02 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-12-20 13:00:22 +0100 |
commit | 1701720dc6c2df030ee5f7db4d8aa6e2422654db (patch) | |
tree | a03ca645d0b98e6c7784de965b3dfc2720dcd35d /src | |
parent | fb587a3a996d2e9d2fca34cc053df29f6d23f64e (diff) | |
download | qtwebchannel-1701720dc6c2df030ee5f7db4d8aa6e2422654db.tar.gz |
Add standalone C++/Qt example which opens HTML client in a browser.
This example shows how to use the (currently quite ugly) raw C++ API
to setup a webchannel without using QML at all. The HTML client is then
handled by the users default browser.
The example itself shows a simple chat between the HTML client and the
C++/Qt server, with a line edit for input and a text edit showing the
chat history.
Change-Id: I8baf14efb9d0c5f5880d99710cf6317fe9b887b9
Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.cpp | 12 | ||||
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.h | 9 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index c141fb2..fcf809a 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -54,6 +54,7 @@ #include <QDebug> #include <QPointer> #include <QEvent> +#include <QJsonDocument> namespace { const QString KEY_SIGNALS = QStringLiteral("signals"); @@ -647,6 +648,17 @@ bool QMetaObjectPublisher::handleRequest(const QJsonObject &message) return false; } +void QMetaObjectPublisher::handleRawMessage(const QString &message) +{ + QJsonParseError error; + const QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error); + if (error.error) { + qWarning() << "Could not parse raw input message as JSON: " << error.errorString() << "Message was: " << message; + } else if (doc.isObject() && !handleRequest(doc.object())) { + qWarning() << "Could not handle raw message as meta object request: " << message; + } +} + QWebChannel *QMetaObjectPublisher::webChannel() const { return d->webChannel; diff --git a/src/webchannel/qmetaobjectpublisher.h b/src/webchannel/qmetaobjectpublisher.h index da733b2..684be1d 100644 --- a/src/webchannel/qmetaobjectpublisher.h +++ b/src/webchannel/qmetaobjectpublisher.h @@ -103,6 +103,15 @@ signals: void webChannelChanged(QWebChannel *channel); void blockUpdatesChanged(bool block); +public slots: + /** + * Helper slot which you can connect directly to WebChannel's rawMessageReceived signal. + * + * This slot then tries to parse the message as JSON and if it succeeds, calls handleRequest + * with the obtained JSON object. + */ + void handleRawMessage(const QString &message); + protected: void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE; |