diff options
Diffstat (limited to 'examples/webchannel/standalone')
-rw-r--r-- | examples/webchannel/standalone/core.h | 91 | ||||
-rw-r--r-- | examples/webchannel/standalone/dialog.cpp | 78 | ||||
-rw-r--r-- | examples/webchannel/standalone/dialog.h | 81 | ||||
-rw-r--r-- | examples/webchannel/standalone/doc/src/standalone.qdoc | 6 | ||||
-rw-r--r-- | examples/webchannel/standalone/index.html | 22 | ||||
-rw-r--r-- | examples/webchannel/standalone/main.cpp | 90 | ||||
-rw-r--r-- | examples/webchannel/standalone/standalone.pro | 3 |
7 files changed, 280 insertions, 91 deletions
diff --git a/examples/webchannel/standalone/core.h b/examples/webchannel/standalone/core.h new file mode 100644 index 0000000..0e91bb7 --- /dev/null +++ b/examples/webchannel/standalone/core.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebChannel module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CORE_H +#define CORE_H + +#include "dialog.h" +#include <QObject> + +/* + An instance of this class gets published over the WebChannel and is then accessible to HTML clients. +*/ +class Core : public QObject +{ + Q_OBJECT + +public: + Core(Dialog *dialog, QObject *parent = nullptr) + : QObject(parent), m_dialog(dialog) + { + connect(dialog, &Dialog::sendText, this, &Core::sendText); + } + +signals: + /* + This signal is emitted from the C++ side and the text displayed on the HTML client side. + */ + void sendText(const QString &text); + +public slots: + + /* + This slot is invoked from the HTML client side and the text displayed on the server side. + */ + void receiveText(const QString &text) + { + m_dialog->displayMessage(Dialog::tr("Received message: %1").arg(text)); + } + +private: + Dialog *m_dialog; +}; + +#endif // CORE_H diff --git a/examples/webchannel/standalone/dialog.cpp b/examples/webchannel/standalone/dialog.cpp new file mode 100644 index 0000000..c9fc164 --- /dev/null +++ b/examples/webchannel/standalone/dialog.cpp @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebChannel module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dialog.h" +#include "ui_dialog.h" + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::Dialog) +{ + ui->setupUi(this); + connect(ui->send, &QPushButton::clicked, this, &Dialog::clicked); +} + +void Dialog::displayMessage(const QString &message) +{ + ui->output->appendPlainText(message); +} + +void Dialog::clicked() +{ + const QString text = ui->input->text(); + + if (text.isEmpty()) + return; + + emit sendText(text); + displayMessage(tr("Sent message: %1").arg(text)); + + ui->input->clear(); +} diff --git a/examples/webchannel/standalone/dialog.h b/examples/webchannel/standalone/dialog.h new file mode 100644 index 0000000..143f2d8 --- /dev/null +++ b/examples/webchannel/standalone/dialog.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebChannel module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DIALOG_H +#define DIALOG_H + +#include <QDialog> + +QT_BEGIN_NAMESPACE +namespace Ui { +class Dialog; +} +QT_END_NAMESPACE + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = nullptr); + + void displayMessage(const QString &message); + +signals: + void sendText(const QString &text); + +private slots: + void clicked(); + +private: + Ui::Dialog *ui; +}; + +#endif // DIALOG_H diff --git a/examples/webchannel/standalone/doc/src/standalone.qdoc b/examples/webchannel/standalone/doc/src/standalone.qdoc index 0d030bf..6777492 100644 --- a/examples/webchannel/standalone/doc/src/standalone.qdoc +++ b/examples/webchannel/standalone/doc/src/standalone.qdoc @@ -64,13 +64,13 @@ \section1 Communicating with a Remote Client - The C++ application sets up a QWebChannel instance and publishes a \c Dialog object over it. + The C++ application sets up a QWebChannel instance and publishes a \c Core object over it. For the remote client side, \l {standalone/index.html}{index.html} is opened. Both show a dialog with the list of received messages and an input box to send messages to the other end. - The \c Dialog emits the \c Dialog::sendText() signal when the user sends a message. The signal + The \c Core emits the \c Core::sendText() signal when the user sends a message. The signal automatically gets propagated to the HTML client. When the user enters a message on the HTML - side, \c Dialog::receiveText() is called. + side, \c Core::receiveText() is called. All communication between the HTML client and the C++ server is done over a WebSocket. The C++ side instantiates a QWebSocketServer and wraps incoming QWebSocket connections diff --git a/examples/webchannel/standalone/index.html b/examples/webchannel/standalone/index.html index b5a9a49..7c042cd 100644 --- a/examples/webchannel/standalone/index.html +++ b/examples/webchannel/standalone/index.html @@ -5,8 +5,7 @@ <script type="text/javascript" src="./qwebchannel.js"></script> <script type="text/javascript"> //BEGIN SETUP - function output(message) - { + function output(message) { var output = document.getElementById("output"); output.innerHTML = output.innerHTML + message + "\n"; } @@ -19,20 +18,17 @@ output("Connecting to WebSocket server at " + baseUrl + "."); var socket = new WebSocket(baseUrl); - socket.onclose = function() - { + socket.onclose = function() { console.error("web channel closed"); }; - socket.onerror = function(error) - { + socket.onerror = function(error) { console.error("web channel error: " + error); }; - socket.onopen = function() - { + socket.onopen = function() { output("WebSocket connected, setting up QWebChannel."); new QWebChannel(socket, function(channel) { - // make dialog object accessible globally - window.dialog = channel.objects.dialog; + // make core object accessible globally + window.core = channel.objects.core; document.getElementById("send").onclick = function() { var input = document.getElementById("input"); @@ -43,14 +39,14 @@ output("Sent message: " + text); input.value = ""; - dialog.receiveText(text); + core.receiveText(text); } - dialog.sendText.connect(function(message) { + core.sendText.connect(function(message) { output("Received message: " + message); }); - dialog.receiveText("Client connected, ready to send/receive messages!"); + core.receiveText("Client connected, ready to send/receive messages!"); output("Connected to WebChannel, ready to send/receive messages!"); }); } diff --git a/examples/webchannel/standalone/main.cpp b/examples/webchannel/standalone/main.cpp index b53e9a6..3ea66ad 100644 --- a/examples/webchannel/standalone/main.cpp +++ b/examples/webchannel/standalone/main.cpp @@ -48,81 +48,19 @@ ** ****************************************************************************/ -#include "qwebchannel.h" +#include "dialog.h" +#include "core.h" +#include "../shared/websocketclientwrapper.h" +#include "../shared/websockettransport.h" #include <QApplication> -#include <QDialog> -#include <QVariantMap> #include <QDesktopServices> -#include <QUrl> +#include <QDialog> #include <QDir> #include <QFileInfo> -#include <QtWebSockets/QWebSocketServer> - -#include "../shared/websocketclientwrapper.h" -#include "../shared/websockettransport.h" - -#include "ui_dialog.h" - -/*! - An instance of this class gets published over the WebChannel and is then accessible to HTML clients. -*/ -class Dialog : public QObject -{ - Q_OBJECT - -public: - explicit Dialog(QObject *parent = 0) - : QObject(parent) - { - ui.setupUi(&dialog); - dialog.show(); - - connect(ui.send, SIGNAL(clicked()), SLOT(clicked())); - } - - void displayMessage(const QString &message) - { - ui.output->appendPlainText(message); - } - -signals: - /*! - This signal is emitted from the C++ side and the text displayed on the HTML client side. - */ - void sendText(const QString &text); - -public slots: - /*! - This slot is invoked from the HTML client side and the text displayed on the server side. - */ - void receiveText(const QString &text) - { - displayMessage(tr("Received message: %1").arg(text)); - } - -private slots: - /*! - Note that this slot is private and thus not accessible to HTML clients. - */ - void clicked() - { - const QString text = ui.input->text(); - - if (text.isEmpty()) { - return; - } - - emit sendText(text); - displayMessage(tr("Sent message: %1").arg(text)); - - ui.input->clear(); - } - -private: - QDialog dialog; - Ui::Dialog ui; -}; +#include <QUrl> +#include <QWebChannel> +#include <QWebSocketServer> int main(int argc, char** argv) { @@ -148,17 +86,19 @@ int main(int argc, char** argv) QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected, &channel, &QWebChannel::connectTo); - // setup the dialog and publish it to the QWebChannel + // setup the UI Dialog dialog; - channel.registerObject(QStringLiteral("dialog"), &dialog); + + // setup the core and publish it to the QWebChannel + Core core(&dialog); + channel.registerObject(QStringLiteral("core"), &core); // open a browser window with the client HTML page QUrl url = QUrl::fromLocalFile(BUILD_DIR "/index.html"); QDesktopServices::openUrl(url); - dialog.displayMessage(QObject::tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString())); + dialog.displayMessage(Dialog::tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString())); + dialog.show(); return app.exec(); } - -#include "main.moc" diff --git a/examples/webchannel/standalone/standalone.pro b/examples/webchannel/standalone/standalone.pro index cfe4297..eea78ef 100644 --- a/examples/webchannel/standalone/standalone.pro +++ b/examples/webchannel/standalone/standalone.pro @@ -4,10 +4,13 @@ CONFIG += warn_on SOURCES += \ main.cpp \ + dialog.cpp \ ../shared/websockettransport.cpp \ ../shared/websocketclientwrapper.cpp HEADERS += \ + core.h \ + dialog.h \ ../shared/websockettransport.h \ ../shared/websocketclientwrapper.h |