summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-07-03 14:57:03 +0200
committerMilian Wolff <milian.wolff@kdab.com>2014-07-04 20:19:27 +0200
commit20937e9192c52630b9b47765b58493ada64fcf65 (patch)
treead21560ea9eec538fab10abe7b6f70bc2c9d3c17 /examples
parent856dc072acb649be03e02ac64c81015d3f2675c2 (diff)
downloadqtwebchannel-20937e9192c52630b9b47765b58493ada64fcf65.tar.gz
Only depend optionally on QtWebSockets, and only use it in the example.
The utility QWebChannelAbstractTransport implementation based on the QtWebSocket has no big value. Instead, it would pull in the QtWebSocket link-time dependency into QtWebKit/QtWebEngine, which is not desired. Considering that the WebSocket usecase is minor, and only few people will ever use it, we agreed that having the code in the example alone is enough. Change-Id: Ica038329a1d684f33e805fc296e9dff71b1446ba Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com> Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/examples.pro4
-rw-r--r--examples/standalone/index.html20
-rw-r--r--examples/standalone/main.cpp73
-rw-r--r--examples/standalone/standalone.pro8
-rw-r--r--examples/standalone/websocketclientwrapper.cpp71
-rw-r--r--examples/standalone/websocketclientwrapper.h71
-rw-r--r--examples/standalone/websockettransport.cpp74
-rw-r--r--examples/standalone/websockettransport.h65
8 files changed, 332 insertions, 54 deletions
diff --git a/examples/examples.pro b/examples/examples.pro
index 114d0fb..5120fef 100644
--- a/examples/examples.pro
+++ b/examples/examples.pro
@@ -1,3 +1,5 @@
TEMPLATE = subdirs
-SUBDIRS += standalone
+qtHaveModule(websockets) {
+ SUBDIRS += standalone
+}
diff --git a/examples/standalone/index.html b/examples/standalone/index.html
index a1aa3e4..e1a74fb 100644
--- a/examples/standalone/index.html
+++ b/examples/standalone/index.html
@@ -5,24 +5,34 @@
<script type="text/javascript" src="../../src/webchannel/qwebchannel.js"></script>
<script type="text/javascript">
//BEGIN SETUP
+ function output(message)
+ {
+ var output = document.getElementById("output");
+ output.innerHTML = output.innerHTML + message + "\n";
+ }
var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
new QWebChannel(baseUrl, function(channel) {
+ // make dialog object accessible globally
+ window.dialog = channel.objects.dialog;
+
document.getElementById("send").onclick = function() {
var input = document.getElementById("input");
var text = input.value;
if (!text) {
return;
}
- var output = document.getElementById("output");
- output.innerHTML = output.innerHTML + "Send message: " + text + "\n";
+
+ output("Sent message: " + text);
input.value = "";
dialog.receiveText(text);
}
- dialog.sendText.connect(function(text) {
- var output = document.getElementById("output");
- output.innerHTML = output.innerHTML + "Received message: " + text + "\n";
+ dialog.sendText.connect(function(message) {
+ output("Received message: " + message);
});
+
+ dialog.receiveText("Client connected, ready to send/receive messages!");
+ output("Connected to WebChannel, ready to send/receive messages!");
});
//END SETUP
</script>
diff --git a/examples/standalone/main.cpp b/examples/standalone/main.cpp
index 05deb24..f407da6 100644
--- a/examples/standalone/main.cpp
+++ b/examples/standalone/main.cpp
@@ -49,8 +49,9 @@
#include <QDebug>
#include <QtWebSockets/QWebSocketServer>
-#include <QtWebSockets/QWebSocket>
-#include <QtWebChannel/QWebChannelWebSocketTransport>
+
+#include "websocketclientwrapper.h"
+#include "websockettransport.h"
#include "ui_dialog.h"
@@ -59,18 +60,18 @@ class Dialog : public QObject
Q_OBJECT
public:
- explicit Dialog(const QString &baseUrl, QObject *parent = 0)
+ explicit Dialog(QObject *parent = 0)
: QObject(parent)
{
ui.setupUi(&dialog);
dialog.show();
connect(ui.send, SIGNAL(clicked()), SLOT(clicked()));
+ }
- QUrl url = QUrl::fromLocalFile(SOURCE_DIR "/index.html");
- url.setQuery(QStringLiteral("webChannelBaseUrl=") + baseUrl);
- ui.output->appendPlainText(tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString()));
- QDesktopServices::openUrl(url);
+ void displayMessage(const QString &message)
+ {
+ ui.output->appendPlainText(message);
}
signals:
@@ -79,7 +80,7 @@ signals:
public slots:
void receiveText(const QString &text)
{
- ui.output->appendPlainText(tr("Received message: %1").arg(text));
+ displayMessage(tr("Received message: %1").arg(text));
}
private slots:
@@ -92,7 +93,7 @@ private slots:
}
emit sendText(text);
- ui.output->appendPlainText(tr("Sent message: %1").arg(text));
+ displayMessage(tr("Sent message: %1").arg(text));
ui.input->clear();
}
@@ -102,53 +103,31 @@ private:
Ui::Dialog ui;
};
-// boiler plate code to connect incoming WebSockets to the WebChannel, such that they receive
-// messages and can access the published objects.
-class TransportHandler : public QObject
-{
- Q_OBJECT
-
-public:
- TransportHandler(QWebChannel *channel, QObject *parent = 0)
- : QObject(parent)
- , m_server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode)
- , m_channel(channel)
- {
- if (!m_server.listen(QHostAddress::LocalHost)) {
- qFatal("Failed to open web socket server.");
- }
-
- connect(&m_server, &QWebSocketServer::newConnection,
- this, &TransportHandler::handleNewConnection);
- }
-
- QString baseUrl() const
- {
- return m_server.serverUrl().toString();
- }
-
-private slots:
- void handleNewConnection()
- {
- m_channel->connectTo(new QWebChannelWebSocketTransport(m_server.nextPendingConnection()));
- }
-
-private:
- QWebSocketServer m_server;
- QWebChannel *m_channel;
-};
-
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QWebChannel channel;
- TransportHandler transportHandler(&channel);
+ QWebSocketServer server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode);
+ if (!server.listen(QHostAddress::LocalHost)) {
+ qFatal("Failed to open web socket server.");
+ return 1;
+ }
- Dialog dialog(transportHandler.baseUrl());
+ WebSocketClientWrapper clientWrapper(&server);
+ QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected,
+ &channel, &QWebChannel::connectTo);
+
+ Dialog dialog;
channel.registerObject(QStringLiteral("dialog"), &dialog);
+ QUrl url = QUrl::fromLocalFile(SOURCE_DIR "/index.html");
+ url.setQuery(QStringLiteral("webChannelBaseUrl=") + server.serverUrl().toString());
+ QDesktopServices::openUrl(url);
+
+ dialog.displayMessage(QObject::tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString()));
+
return app.exec();
}
diff --git a/examples/standalone/standalone.pro b/examples/standalone/standalone.pro
index 114be58..37b6de3 100644
--- a/examples/standalone/standalone.pro
+++ b/examples/standalone/standalone.pro
@@ -3,7 +3,13 @@ QT += gui webchannel widgets websockets
CONFIG += warn_on
SOURCES += \
- main.cpp
+ main.cpp \
+ websockettransport.cpp \
+ websocketclientwrapper.cpp
+
+HEADERS += \
+ websockettransport.h \
+ websocketclientwrapper.h
FORMS += \
dialog.ui
diff --git a/examples/standalone/websocketclientwrapper.cpp b/examples/standalone/websocketclientwrapper.cpp
new file mode 100644
index 0000000..219050d
--- /dev/null
+++ b/examples/standalone/websocketclientwrapper.cpp
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "websocketclientwrapper.h"
+
+#include <QtWebSockets/QWebSocketServer>
+
+#include "websockettransport.h"
+
+/*!
+ \brief Wrapps connected QWebSockets clients in WebSocketTransport objects.
+
+ This code is all that is required to connect incoming WebSockets to the WebChannel. Any kind
+ of remote JavaScript client that supports WebSockets can thus receive messages and access the
+ published objects.
+*/
+
+QT_BEGIN_NAMESPACE
+
+WebSocketClientWrapper::WebSocketClientWrapper(QWebSocketServer *server, QObject *parent)
+ : QObject(parent)
+ , m_server(server)
+{
+ connect(server, &QWebSocketServer::newConnection,
+ this, &WebSocketClientWrapper::handleNewConnection);
+}
+
+void WebSocketClientWrapper::handleNewConnection()
+{
+ emit clientConnected(new WebSocketTransport(m_server->nextPendingConnection()));
+}
+
+QT_END_NAMESPACE
diff --git a/examples/standalone/websocketclientwrapper.h b/examples/standalone/websocketclientwrapper.h
new file mode 100644
index 0000000..1f742f7
--- /dev/null
+++ b/examples/standalone/websocketclientwrapper.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef WEBSOCKETTRANSPORTSERVER_H
+#define WEBSOCKETTRANSPORTSERVER_H
+
+#include <QObject>
+
+QT_BEGIN_NAMESPACE
+
+class QWebSocketServer;
+class WebSocketTransport;
+
+class WebSocketClientWrapper : public QObject
+{
+ Q_OBJECT
+
+public:
+ WebSocketClientWrapper(QWebSocketServer *server, QObject *parent = 0);
+
+Q_SIGNALS:
+ void clientConnected(WebSocketTransport* client);
+
+private Q_SLOTS:
+ void handleNewConnection();
+
+private:
+ QWebSocketServer *m_server;
+};
+
+QT_END_NAMESPACE
+
+#endif // WEBSOCKETTRANSPORTSERVER_H
diff --git a/examples/standalone/websockettransport.cpp b/examples/standalone/websockettransport.cpp
new file mode 100644
index 0000000..5df9f85
--- /dev/null
+++ b/examples/standalone/websockettransport.cpp
@@ -0,0 +1,74 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "websockettransport.h"
+
+#include <QtWebSockets/QWebSocket>
+
+/*!
+ \brief QWebChannelAbstractSocket implementation that uses a QWebSocket internally.
+
+ The transport delegates all messages received over the QWebSocket over its
+ textMessageReceived signal. Analogously, all calls to sendTextMessage will
+ be send over the QWebSocket to the remote client.
+*/
+
+QT_BEGIN_NAMESPACE
+
+WebSocketTransport::WebSocketTransport(QWebSocket *socket)
+: QWebChannelAbstractTransport(socket)
+, m_socket(socket)
+{
+ connect(socket, &QWebSocket::textMessageReceived,
+ this, &WebSocketTransport::textMessageReceived);
+}
+
+WebSocketTransport::~WebSocketTransport()
+{
+
+}
+
+void WebSocketTransport::sendTextMessage(const QString &message)
+{
+ m_socket->sendTextMessage(message);
+}
+
+QT_END_NAMESPACE
diff --git a/examples/standalone/websockettransport.h b/examples/standalone/websockettransport.h
new file mode 100644
index 0000000..5fab8f6
--- /dev/null
+++ b/examples/standalone/websockettransport.h
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebChannel module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef WEBSOCKETTRANSPORT_H
+#define WEBSOCKETTRANSPORT_H
+
+#include <QtWebChannel/QWebChannelAbstractTransport>
+
+QT_BEGIN_NAMESPACE
+
+class QWebSocket;
+class WebSocketTransport : public QWebChannelAbstractTransport
+{
+ Q_OBJECT
+public:
+ explicit WebSocketTransport(QWebSocket *socket);
+ virtual ~WebSocketTransport();
+
+ void sendTextMessage(const QString &message) Q_DECL_OVERRIDE;
+
+private:
+ QWebSocket *m_socket;
+};
+
+QT_END_NAMESPACE
+
+#endif // WEBSOCKETTRANSPORT_H