summaryrefslogtreecommitdiff
path: root/examples/standalone
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-07-10 00:05:28 +0200
committerMilian Wolff <milian.wolff@kdab.com>2014-07-29 13:58:04 +0200
commit94912cf26ba70a2cadd23f1c931395be64c11eac (patch)
tree28cfa1d98bbcc7eeccba6de1e3317e30add56a2f /examples/standalone
parent77a4c34bb0f3b8cc3107f9fc6be0f5f7b23bb9d6 (diff)
downloadqtwebchannel-94912cf26ba70a2cadd23f1c931395be64c11eac.tar.gz
Refactor and streamline API and IPC protocol.
This patch removes the obsolete API support to send raw messages using a QWebChannel. Instead, it is encouraged to directly use WebSockets or navigator.qt. By doing so, we can cleanup the code considerably. While at it, the transport API is adapted to work on QJsonObject messages, instead of QStrings. This will allow us to use more efficient formats in e.g. QtWebKit or QtWebEngine. One could also implement a JSONRPC interface using a custom transport then. Change-Id: Ia8c125a5558507b3cbecf128a46b19fdb013f47b Reviewed-by: Allan Sandfeld Jensen <allan.jensen@digia.com>
Diffstat (limited to 'examples/standalone')
-rw-r--r--examples/standalone/websockettransport.cpp24
-rw-r--r--examples/standalone/websockettransport.h5
2 files changed, 26 insertions, 3 deletions
diff --git a/examples/standalone/websockettransport.cpp b/examples/standalone/websockettransport.cpp
index 5df9f85..f1ecee2 100644
--- a/examples/standalone/websockettransport.cpp
+++ b/examples/standalone/websockettransport.cpp
@@ -41,6 +41,10 @@
#include "websockettransport.h"
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QDebug>
+
#include <QtWebSockets/QWebSocket>
/*!
@@ -66,9 +70,25 @@ WebSocketTransport::~WebSocketTransport()
}
-void WebSocketTransport::sendTextMessage(const QString &message)
+void WebSocketTransport::sendMessage(const QJsonObject &message)
+{
+ QJsonDocument doc(message);
+ m_socket->sendTextMessage(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
+}
+
+void WebSocketTransport::textMessageReceived(const QString &messageData)
{
- m_socket->sendTextMessage(message);
+ QJsonParseError error;
+ QJsonDocument message = QJsonDocument::fromJson(messageData.toUtf8(), &error);
+ if (error.error) {
+ qWarning() << "Failed to parse text message as JSON object:" << messageData
+ << "Error is:" << error.errorString();
+ return;
+ } else if (!message.isObject()) {
+ qWarning() << "Received JSON message that is not an object: " << messageData;
+ return;
+ }
+ emit messageReceived(message.object(), this);
}
QT_END_NAMESPACE
diff --git a/examples/standalone/websockettransport.h b/examples/standalone/websockettransport.h
index 5fab8f6..828ac00 100644
--- a/examples/standalone/websockettransport.h
+++ b/examples/standalone/websockettransport.h
@@ -54,7 +54,10 @@ public:
explicit WebSocketTransport(QWebSocket *socket);
virtual ~WebSocketTransport();
- void sendTextMessage(const QString &message) Q_DECL_OVERRIDE;
+ void sendMessage(const QJsonObject &message) Q_DECL_OVERRIDE;
+
+private Q_SLOTS:
+ void textMessageReceived(const QString &message);
private:
QWebSocket *m_socket;