diff options
-rw-r--r-- | examples/examples.pro | 4 | ||||
-rw-r--r-- | examples/standalone/index.html | 20 | ||||
-rw-r--r-- | examples/standalone/main.cpp | 73 | ||||
-rw-r--r-- | examples/standalone/standalone.pro | 8 | ||||
-rw-r--r-- | examples/standalone/websocketclientwrapper.cpp | 71 | ||||
-rw-r--r-- | examples/standalone/websocketclientwrapper.h | 71 | ||||
-rw-r--r-- | examples/standalone/websockettransport.cpp (renamed from src/webchannel/qwebchannelwebsockettransport.cpp) | 23 | ||||
-rw-r--r-- | examples/standalone/websockettransport.h (renamed from src/webchannel/qwebchannelwebsockettransport.h) | 19 | ||||
-rw-r--r-- | src/webchannel/webchannel.pro | 8 | ||||
-rw-r--r-- | sync.profile | 2 |
10 files changed, 214 insertions, 85 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/src/webchannel/qwebchannelwebsockettransport.cpp b/examples/standalone/websockettransport.cpp index 59c9538..5df9f85 100644 --- a/src/webchannel/qwebchannelwebsockettransport.cpp +++ b/examples/standalone/websockettransport.cpp @@ -39,10 +39,11 @@ ** ****************************************************************************/ -#include "qwebchannelwebsockettransport.h" +#include "websockettransport.h" + +#include <QtWebSockets/QWebSocket> /*! - \inmodule QtWebChannel \brief QWebChannelAbstractSocket implementation that uses a QWebSocket internally. The transport delegates all messages received over the QWebSocket over its @@ -52,28 +53,22 @@ QT_BEGIN_NAMESPACE -struct QWebChannelWebSocketTransportPrivate -{ - QWebSocket *socket; -}; - -QWebChannelWebSocketTransport::QWebChannelWebSocketTransport(QWebSocket *socket) +WebSocketTransport::WebSocketTransport(QWebSocket *socket) : QWebChannelAbstractTransport(socket) -, d(new QWebChannelWebSocketTransportPrivate) +, m_socket(socket) { - d->socket = socket; connect(socket, &QWebSocket::textMessageReceived, - this, &QWebChannelWebSocketTransport::textMessageReceived); + this, &WebSocketTransport::textMessageReceived); } -QWebChannelWebSocketTransport::~QWebChannelWebSocketTransport() +WebSocketTransport::~WebSocketTransport() { } -void QWebChannelWebSocketTransport::sendTextMessage(const QString &message) +void WebSocketTransport::sendTextMessage(const QString &message) { - d->socket->sendTextMessage(message); + m_socket->sendTextMessage(message); } QT_END_NAMESPACE diff --git a/src/webchannel/qwebchannelwebsockettransport.h b/examples/standalone/websockettransport.h index b718b9b..5fab8f6 100644 --- a/src/webchannel/qwebchannelwebsockettransport.h +++ b/examples/standalone/websockettransport.h @@ -39,30 +39,27 @@ ** ****************************************************************************/ -#ifndef QWEBCHANNELWEBSOCKETTRANSPORT_H -#define QWEBCHANNELWEBSOCKETTRANSPORT_H +#ifndef WEBSOCKETTRANSPORT_H +#define WEBSOCKETTRANSPORT_H -#include <QObject> #include <QtWebChannel/QWebChannelAbstractTransport> -#include <QtWebChannel/qwebchannelglobal.h> -#include <QtWebSockets/QWebSocket> QT_BEGIN_NAMESPACE -struct QWebChannelWebSocketTransportPrivate; -class Q_WEBCHANNEL_EXPORT QWebChannelWebSocketTransport : public QWebChannelAbstractTransport +class QWebSocket; +class WebSocketTransport : public QWebChannelAbstractTransport { Q_OBJECT public: - explicit QWebChannelWebSocketTransport(QWebSocket *socket); - virtual ~QWebChannelWebSocketTransport(); + explicit WebSocketTransport(QWebSocket *socket); + virtual ~WebSocketTransport(); void sendTextMessage(const QString &message) Q_DECL_OVERRIDE; private: - QScopedPointer<QWebChannelWebSocketTransportPrivate> d; + QWebSocket *m_socket; }; QT_END_NAMESPACE -#endif // QWEBCHANNELWEBSOCKETTRANSPORT_H +#endif // WEBSOCKETTRANSPORT_H diff --git a/src/webchannel/webchannel.pro b/src/webchannel/webchannel.pro index a50beaa..086deeb 100644 --- a/src/webchannel/webchannel.pro +++ b/src/webchannel/webchannel.pro @@ -1,5 +1,5 @@ TARGET = QtWebChannel -QT = core network websockets +QT = core network CONFIG += warn_on strict_flags load(qt_module) @@ -12,8 +12,7 @@ OTHER_FILES = \ PUBLIC_HEADERS += \ qwebchannel.h \ - qwebchannelabstracttransport.h \ - qwebchannelwebsockettransport.h + qwebchannelabstracttransport.h PRIVATE_HEADERS += \ qwebchannel_p.h \ @@ -24,8 +23,7 @@ PRIVATE_HEADERS += \ SOURCES += \ qwebchannel.cpp \ qmetaobjectpublisher.cpp \ - qwebchannelabstracttransport.cpp \ - qwebchannelwebsockettransport.cpp + qwebchannelabstracttransport.cpp qtHaveModule(qml) { QT += qml diff --git a/sync.profile b/sync.profile index f0e6c33..6c0ef84 100644 --- a/sync.profile +++ b/sync.profile @@ -12,7 +12,7 @@ # %dependencies = ( "qtbase" => "refs/heads/dev", - "qtwebsockets" => "refs/heads/dev", # optional dependencies: + "qtwebsockets" => "refs/heads/dev", "qtdeclarative" => "refs/heads/dev", ); |