From 62760f579b52fbd19d85d31fa90274f9b0dc5111 Mon Sep 17 00:00:00 2001 From: Topi Reinio Date: Wed, 13 Aug 2014 14:14:49 +0200 Subject: Doc: Fix example documentation and paths To fix issues related to example documentation and to follow Qt convention, do the following changes: - Rename examples/qwebchannel to examples/webchannel - Move example-specific documentation to correct location(s) - Include generic 'Running the Example' instructions - Add the module name to example title, fix links This ensures that example docs are built and the example manifest file generated correctly. Change-Id: I284e0b13db95a6738d72258735018b59156cc7da Reviewed-by: Milian Wolff --- examples/examples.pro | 2 +- examples/qwebchannel/doc/examples.qdoc | 49 ------- examples/qwebchannel/doc/standalone-screenshot.png | Bin 30363 -> 0 bytes examples/qwebchannel/doc/standalone.qdoc | 64 --------- examples/qwebchannel/exampleassets.pri | 31 ----- examples/qwebchannel/nodejs/README | 14 -- examples/qwebchannel/nodejs/chatclient.js | 133 ------------------ examples/qwebchannel/nodejs/nodejs.pro | 7 - examples/qwebchannel/nodejs/package.json | 10 -- examples/qwebchannel/qwebchannel.pro | 7 - examples/qwebchannel/standalone/dialog.ui | 48 ------- examples/qwebchannel/standalone/index.html | 79 ----------- examples/qwebchannel/standalone/main.cpp | 151 --------------------- examples/qwebchannel/standalone/standalone.pro | 22 --- .../standalone/websocketclientwrapper.cpp | 80 ----------- .../standalone/websocketclientwrapper.h | 71 ---------- .../qwebchannel/standalone/websockettransport.cpp | 108 --------------- .../qwebchannel/standalone/websockettransport.h | 68 ---------- examples/webchannel/exampleassets.pri | 31 +++++ examples/webchannel/nodejs/README | 14 ++ examples/webchannel/nodejs/chatclient.js | 133 ++++++++++++++++++ examples/webchannel/nodejs/nodejs.pro | 7 + examples/webchannel/nodejs/package.json | 10 ++ examples/webchannel/standalone/dialog.ui | 48 +++++++ .../doc/images/standalone-screenshot.png | Bin 0 -> 30363 bytes .../webchannel/standalone/doc/src/standalone.qdoc | 68 ++++++++++ examples/webchannel/standalone/index.html | 79 +++++++++++ examples/webchannel/standalone/main.cpp | 151 +++++++++++++++++++++ examples/webchannel/standalone/standalone.pro | 22 +++ .../standalone/websocketclientwrapper.cpp | 80 +++++++++++ .../webchannel/standalone/websocketclientwrapper.h | 71 ++++++++++ .../webchannel/standalone/websockettransport.cpp | 108 +++++++++++++++ .../webchannel/standalone/websockettransport.h | 68 ++++++++++ examples/webchannel/webchannel.pro | 7 + src/webchannel/doc/src/examples.qdoc | 49 +++++++ src/webchannel/doc/src/javascript.qdoc | 3 +- src/webchannel/qwebchannel.cpp | 2 +- src/webchannel/qwebchannelabstracttransport.cpp | 8 +- 38 files changed, 954 insertions(+), 949 deletions(-) delete mode 100644 examples/qwebchannel/doc/examples.qdoc delete mode 100644 examples/qwebchannel/doc/standalone-screenshot.png delete mode 100644 examples/qwebchannel/doc/standalone.qdoc delete mode 100644 examples/qwebchannel/exampleassets.pri delete mode 100644 examples/qwebchannel/nodejs/README delete mode 100644 examples/qwebchannel/nodejs/chatclient.js delete mode 100644 examples/qwebchannel/nodejs/nodejs.pro delete mode 100644 examples/qwebchannel/nodejs/package.json delete mode 100644 examples/qwebchannel/qwebchannel.pro delete mode 100644 examples/qwebchannel/standalone/dialog.ui delete mode 100644 examples/qwebchannel/standalone/index.html delete mode 100644 examples/qwebchannel/standalone/main.cpp delete mode 100644 examples/qwebchannel/standalone/standalone.pro delete mode 100644 examples/qwebchannel/standalone/websocketclientwrapper.cpp delete mode 100644 examples/qwebchannel/standalone/websocketclientwrapper.h delete mode 100644 examples/qwebchannel/standalone/websockettransport.cpp delete mode 100644 examples/qwebchannel/standalone/websockettransport.h create mode 100644 examples/webchannel/exampleassets.pri create mode 100644 examples/webchannel/nodejs/README create mode 100644 examples/webchannel/nodejs/chatclient.js create mode 100644 examples/webchannel/nodejs/nodejs.pro create mode 100644 examples/webchannel/nodejs/package.json create mode 100644 examples/webchannel/standalone/dialog.ui create mode 100644 examples/webchannel/standalone/doc/images/standalone-screenshot.png create mode 100644 examples/webchannel/standalone/doc/src/standalone.qdoc create mode 100644 examples/webchannel/standalone/index.html create mode 100644 examples/webchannel/standalone/main.cpp create mode 100644 examples/webchannel/standalone/standalone.pro create mode 100644 examples/webchannel/standalone/websocketclientwrapper.cpp create mode 100644 examples/webchannel/standalone/websocketclientwrapper.h create mode 100644 examples/webchannel/standalone/websockettransport.cpp create mode 100644 examples/webchannel/standalone/websockettransport.h create mode 100644 examples/webchannel/webchannel.pro create mode 100644 src/webchannel/doc/src/examples.qdoc diff --git a/examples/examples.pro b/examples/examples.pro index 1cb6c9f..657e144 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs -SUBDIRS += qwebchannel +SUBDIRS += webchannel diff --git a/examples/qwebchannel/doc/examples.qdoc b/examples/qwebchannel/doc/examples.qdoc deleted file mode 100644 index 613d760..0000000 --- a/examples/qwebchannel/doc/examples.qdoc +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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$ -** -****************************************************************************/ - -/*! - \title Qt WebChannel Examples - \group qtwebchannel-examples - \brief List of Qt WebChannel examples. - - The examples show how to use the QWebChannel C++ and WebChannel QML API. - -*/ diff --git a/examples/qwebchannel/doc/standalone-screenshot.png b/examples/qwebchannel/doc/standalone-screenshot.png deleted file mode 100644 index 3c55e19..0000000 Binary files a/examples/qwebchannel/doc/standalone-screenshot.png and /dev/null differ diff --git a/examples/qwebchannel/doc/standalone.qdoc b/examples/qwebchannel/doc/standalone.qdoc deleted file mode 100644 index 1d81dad..0000000 --- a/examples/qwebchannel/doc/standalone.qdoc +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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$ -** -****************************************************************************/ - -/*! - \example standalone - \title Standalone Example - \ingroup qtwebchannel-examples - \image standalone-screenshot.png - \brief Shows how to use the QWebChannel C++ API to communicate with an external client. - - The standalone example is a simple chat between a pure C++/Qt application and a remote HTML - client running in your default browser. - - The C++ app sets up a QWebChannel and publishes a Dialog object over it. - For the remote client side, \c 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 Dialog emits the Dialog::sendText() signal when the user sents a message. The signal - automatically gets propagated to the HTML client. When the user enters a message on the HTML - side, Dialog::receiveText() is called. - - All communication between the HTML client and the C++/Qt server is done over a WebSocket. - The C++ side instantiates a QWebSocketServer and wraps incoming QWebSocket connections - in WebSocketTransport objects, which implement QWebChannelAbstractTransport. These objects are - then connected to the QWebChannel instance. -*/ diff --git a/examples/qwebchannel/exampleassets.pri b/examples/qwebchannel/exampleassets.pri deleted file mode 100644 index 035ca12..0000000 --- a/examples/qwebchannel/exampleassets.pri +++ /dev/null @@ -1,31 +0,0 @@ -# This adds the qwebchannel js library to an example, creating a self-contained bundle -QTDIR_build { - # Build from within Qt. Copy and install the reference lib. - jslib = $$dirname(_QMAKE_CONF_)/src/webchannel/qwebchannel.js - copyfiles = $$jslib -} else { - # This is what an actual 3rd party project would do. - jslib = qwebchannel.js -} - -# This installs all assets including qwebchannel.js, regardless of the source. -exampleassets.files += $$jslib -INSTALLS += exampleassets - -# This code ensures that all assets are present in the build directory. - -!equals(_PRO_FILE_PWD_, $$OUT_PWD) { - # Shadow build, copy all example assets. - copyfiles = $$exampleassets.files -} - -defineReplace(stripSrcDir) { - return($$basename(1)) -} - -assetcopy.input = copyfiles -assetcopy.output = $$OUT_PWD/${QMAKE_FUNC_FILE_IN_stripSrcDir} -assetcopy.commands = $$QMAKE_COPY ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT} -assetcopy.name = COPY ${QMAKE_FILE_IN} -assetcopy.CONFIG = no_link target_predeps -QMAKE_EXTRA_COMPILERS += assetcopy diff --git a/examples/qwebchannel/nodejs/README b/examples/qwebchannel/nodejs/README deleted file mode 100644 index 07f8fd8..0000000 --- a/examples/qwebchannel/nodejs/README +++ /dev/null @@ -1,14 +0,0 @@ -This is a small NodeJS implementation on how to connect to a QWebChannel. - -This example implements a small chat client using the command line. It connects -to the server provided by the other example 'examples/standalone/standalone'. Of -course the port the server listens on must be known. - -How to run it: - -- Install NodeJS (at least 0.10.x) (http://www.nodejs.org/download/) -- go to examples/nodejs and execute "npm install". This sets up the nodeJS - environment in this folder -- ensure examples/standalone/standalone is running -- execute the example with "node chatclient.js" - diff --git a/examples/qwebchannel/nodejs/chatclient.js b/examples/qwebchannel/nodejs/chatclient.js deleted file mode 100644 index 03ce214..0000000 --- a/examples/qwebchannel/nodejs/chatclient.js +++ /dev/null @@ -1,133 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Copyright (C) 2014 basysKom GmbH, author Bernd Lamecker -** 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$ -** -****************************************************************************/ -'use strict'; - -var readline = require('readline'); -var QWebChannel = require('./qwebchannel.js').QWebChannel; -var websocket = require('faye-websocket'); - -var address = 'ws://127.0.0.1:12345'; -var socket = new websocket.Client(address); - -console.log('Chat client connecting to ' + address + ' (Ctrl-D to quit)'); - -var createReadlineInterface = function() { - var bye = function() { - console.log('Bye...'); - process.exit(0); - } - - var rlif = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - rlif.setPrompt('chat: '); - - // Handle Ctrl-D and Ctrl-C - rlif.on('close', bye); - rlif.on('SIGINT', bye); - - return rlif; -} - -var createWebChannel = function(transport, rlif) { - return new QWebChannel(transport, function(channel) { - // We connect to the 'sendText' signal of the remote QObject - // Be aware, that the signal is named for the remote side, - // i.e. the server wants to 'send text'. - // This can be confusing, as we connect to the signal - // to receive incoming messages on our side - channel.objects.dialog.sendText.connect(function(message) { - process.stdout.cursorTo(0); - process.stdout.clearLine(0); - console.log(' << ' + message); - rlif.prompt(); - // Go to end of existing input if any - rlif.write(null, {ctrl: true, name: 'e'}) - }); - - rlif.on('line', function(line) { - var l = line.trim(); - if (l !== '') { - process.stdout.moveCursor(0, -1); - process.stdout.cursorTo(0); - process.stdout.clearLine(0); - // The 'receiveText' slot of the remote QObject - // is called with our message. - // Again the naming is for the server side, - // i.e. the slot is used _by the server_ to receive text. - channel.objects.dialog.receiveText(l); - console.log(' >> ' + l); - } - rlif.prompt(); - }); - rlif.prompt(); - }); -} - -socket.on('open', function(event) { - console.log("info: Client connected"); - var transport = { - // We cant't do 'send: socket.send' here - // because 'send' wouldn't be bound to 'socket' - send: function(data) {socket.send(data)} - }; - - createWebChannel(transport, createReadlineInterface()); - - // QWebChannel has set up its onmessage handler - // on the transport in the constructor. - // Now we connect it to the websocket event. - socket.on('message', function(event) { - transport.onmessage(event); - }); -}); - -socket.on('error', function (error) { - console.log('Connection error: ' + error.message); - process.exit(1); -}); - -socket.on('close', function () { - console.log('Connection closed.'); - process.exit(1); -}); diff --git a/examples/qwebchannel/nodejs/nodejs.pro b/examples/qwebchannel/nodejs/nodejs.pro deleted file mode 100644 index 4df94d2..0000000 --- a/examples/qwebchannel/nodejs/nodejs.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = aux - -exampleassets.files += \ - chatclient.js \ - package.json -exampleassets.path = $$[QT_INSTALL_EXAMPLES]/qwebchannel/nodejs -include(../exampleassets.pri) diff --git a/examples/qwebchannel/nodejs/package.json b/examples/qwebchannel/nodejs/package.json deleted file mode 100644 index 2d863cc..0000000 --- a/examples/qwebchannel/nodejs/package.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "Chatclient", - "description": "Chatclient example", - "author": "basysKom ", - "version": "0.0.1", - "dependencies": { - "faye-websocket": "0.7.x" - }, - "engine": "node 0.10.x" -} diff --git a/examples/qwebchannel/qwebchannel.pro b/examples/qwebchannel/qwebchannel.pro deleted file mode 100644 index de6b4b8..0000000 --- a/examples/qwebchannel/qwebchannel.pro +++ /dev/null @@ -1,7 +0,0 @@ -TEMPLATE = subdirs - -qtHaveModule(widgets):qtHaveModule(websockets) { - SUBDIRS += standalone -} - -SUBDIRS += nodejs diff --git a/examples/qwebchannel/standalone/dialog.ui b/examples/qwebchannel/standalone/dialog.ui deleted file mode 100644 index 056a3f5..0000000 --- a/examples/qwebchannel/standalone/dialog.ui +++ /dev/null @@ -1,48 +0,0 @@ - - - Dialog - - - - 0 - 0 - 400 - 300 - - - - Dialog - - - - - - Message Contents - - - - - - - Send - - - - - - - true - - - Initializing WebChannel... - - - false - - - - - - - - diff --git a/examples/qwebchannel/standalone/index.html b/examples/qwebchannel/standalone/index.html deleted file mode 100644 index 778a502..0000000 --- a/examples/qwebchannel/standalone/index.html +++ /dev/null @@ -1,79 +0,0 @@ - - - - - - - - - -
- - - diff --git a/examples/qwebchannel/standalone/main.cpp b/examples/qwebchannel/standalone/main.cpp deleted file mode 100644 index 2cbc6b8..0000000 --- a/examples/qwebchannel/standalone/main.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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 "qwebchannel.h" - -#include -#include -#include -#include -#include -#include - -#include - -#include "websocketclientwrapper.h" -#include "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; -}; - -int main(int argc, char** argv) -{ - QApplication app(argc, argv); - - // setup the QWebSocketServer - QWebSocketServer server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode); - if (!server.listen(QHostAddress::LocalHost, 12345)) { - qFatal("Failed to open web socket server."); - return 1; - } - - // wrap WebSocket clients in QWebChannelAbstractTransport objects - WebSocketClientWrapper clientWrapper(&server); - - // setup the channel - QWebChannel channel; - QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected, - &channel, &QWebChannel::connectTo); - - // setup the dialog and publish it to the QWebChannel - Dialog dialog; - channel.registerObject(QStringLiteral("dialog"), &dialog); - - // open a browser window with the client HTML page - QUrl url = QUrl::fromLocalFile(BUILD_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(); -} - -#include "main.moc" diff --git a/examples/qwebchannel/standalone/standalone.pro b/examples/qwebchannel/standalone/standalone.pro deleted file mode 100644 index fdeb87a..0000000 --- a/examples/qwebchannel/standalone/standalone.pro +++ /dev/null @@ -1,22 +0,0 @@ -QT += gui webchannel widgets websockets - -CONFIG += warn_on - -SOURCES += \ - main.cpp \ - websockettransport.cpp \ - websocketclientwrapper.cpp - -HEADERS += \ - websockettransport.h \ - websocketclientwrapper.h - -FORMS += \ - dialog.ui - -DEFINES += "BUILD_DIR=\"\\\""$$OUT_PWD"\\\"\"" - -exampleassets.files += \ - index.html -exampleassets.path = $$[QT_INSTALL_EXAMPLES]/qwebchannel/standalone -include(../exampleassets.pri) diff --git a/examples/qwebchannel/standalone/websocketclientwrapper.cpp b/examples/qwebchannel/standalone/websocketclientwrapper.cpp deleted file mode 100644 index 0e776e1..0000000 --- a/examples/qwebchannel/standalone/websocketclientwrapper.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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 - -#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 - -/*! - Construct the client wrapper with the given parent. - - All clients connecting to the QWebSocketServer will be automatically wrapped - in WebSocketTransport objects. -*/ -WebSocketClientWrapper::WebSocketClientWrapper(QWebSocketServer *server, QObject *parent) - : QObject(parent) - , m_server(server) -{ - connect(server, &QWebSocketServer::newConnection, - this, &WebSocketClientWrapper::handleNewConnection); -} - -/*! - Wrap an incoming WebSocket connection in a WebSocketTransport object. -*/ -void WebSocketClientWrapper::handleNewConnection() -{ - emit clientConnected(new WebSocketTransport(m_server->nextPendingConnection())); -} - -QT_END_NAMESPACE diff --git a/examples/qwebchannel/standalone/websocketclientwrapper.h b/examples/qwebchannel/standalone/websocketclientwrapper.h deleted file mode 100644 index 1f742f7..0000000 --- a/examples/qwebchannel/standalone/websocketclientwrapper.h +++ /dev/null @@ -1,71 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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 - -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/qwebchannel/standalone/websockettransport.cpp b/examples/qwebchannel/standalone/websockettransport.cpp deleted file mode 100644 index 5714463..0000000 --- a/examples/qwebchannel/standalone/websockettransport.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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 -#include -#include - -#include - -/*! - \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 - -/*! - Construct the transport object and wrap the given socket. - - The socket is also set as the parent of the transport object. -*/ -WebSocketTransport::WebSocketTransport(QWebSocket *socket) -: QWebChannelAbstractTransport(socket) -, m_socket(socket) -{ - connect(socket, &QWebSocket::textMessageReceived, - this, &WebSocketTransport::textMessageReceived); -} - -/*! - Destroys the WebSocketTransport. -*/ -WebSocketTransport::~WebSocketTransport() -{ - -} - -/*! - Serialize the JSON message and send it as a text message via the WebSocket to the client. -*/ -void WebSocketTransport::sendMessage(const QJsonObject &message) -{ - QJsonDocument doc(message); - m_socket->sendTextMessage(QString::fromUtf8(doc.toJson(QJsonDocument::Compact))); -} - -/*! - Deserialize the stringified JSON messageData and emit messageReceived. -*/ -void WebSocketTransport::textMessageReceived(const QString &messageData) -{ - 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/qwebchannel/standalone/websockettransport.h b/examples/qwebchannel/standalone/websockettransport.h deleted file mode 100644 index 828ac00..0000000 --- a/examples/qwebchannel/standalone/websockettransport.h +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -** 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 - -QT_BEGIN_NAMESPACE - -class QWebSocket; -class WebSocketTransport : public QWebChannelAbstractTransport -{ - Q_OBJECT -public: - explicit WebSocketTransport(QWebSocket *socket); - virtual ~WebSocketTransport(); - - void sendMessage(const QJsonObject &message) Q_DECL_OVERRIDE; - -private Q_SLOTS: - void textMessageReceived(const QString &message); - -private: - QWebSocket *m_socket; -}; - -QT_END_NAMESPACE - -#endif // WEBSOCKETTRANSPORT_H diff --git a/examples/webchannel/exampleassets.pri b/examples/webchannel/exampleassets.pri new file mode 100644 index 0000000..035ca12 --- /dev/null +++ b/examples/webchannel/exampleassets.pri @@ -0,0 +1,31 @@ +# This adds the qwebchannel js library to an example, creating a self-contained bundle +QTDIR_build { + # Build from within Qt. Copy and install the reference lib. + jslib = $$dirname(_QMAKE_CONF_)/src/webchannel/qwebchannel.js + copyfiles = $$jslib +} else { + # This is what an actual 3rd party project would do. + jslib = qwebchannel.js +} + +# This installs all assets including qwebchannel.js, regardless of the source. +exampleassets.files += $$jslib +INSTALLS += exampleassets + +# This code ensures that all assets are present in the build directory. + +!equals(_PRO_FILE_PWD_, $$OUT_PWD) { + # Shadow build, copy all example assets. + copyfiles = $$exampleassets.files +} + +defineReplace(stripSrcDir) { + return($$basename(1)) +} + +assetcopy.input = copyfiles +assetcopy.output = $$OUT_PWD/${QMAKE_FUNC_FILE_IN_stripSrcDir} +assetcopy.commands = $$QMAKE_COPY ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT} +assetcopy.name = COPY ${QMAKE_FILE_IN} +assetcopy.CONFIG = no_link target_predeps +QMAKE_EXTRA_COMPILERS += assetcopy diff --git a/examples/webchannel/nodejs/README b/examples/webchannel/nodejs/README new file mode 100644 index 0000000..07f8fd8 --- /dev/null +++ b/examples/webchannel/nodejs/README @@ -0,0 +1,14 @@ +This is a small NodeJS implementation on how to connect to a QWebChannel. + +This example implements a small chat client using the command line. It connects +to the server provided by the other example 'examples/standalone/standalone'. Of +course the port the server listens on must be known. + +How to run it: + +- Install NodeJS (at least 0.10.x) (http://www.nodejs.org/download/) +- go to examples/nodejs and execute "npm install". This sets up the nodeJS + environment in this folder +- ensure examples/standalone/standalone is running +- execute the example with "node chatclient.js" + diff --git a/examples/webchannel/nodejs/chatclient.js b/examples/webchannel/nodejs/chatclient.js new file mode 100644 index 0000000..03ce214 --- /dev/null +++ b/examples/webchannel/nodejs/chatclient.js @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 basysKom GmbH, author Bernd Lamecker +** 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$ +** +****************************************************************************/ +'use strict'; + +var readline = require('readline'); +var QWebChannel = require('./qwebchannel.js').QWebChannel; +var websocket = require('faye-websocket'); + +var address = 'ws://127.0.0.1:12345'; +var socket = new websocket.Client(address); + +console.log('Chat client connecting to ' + address + ' (Ctrl-D to quit)'); + +var createReadlineInterface = function() { + var bye = function() { + console.log('Bye...'); + process.exit(0); + } + + var rlif = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + rlif.setPrompt('chat: '); + + // Handle Ctrl-D and Ctrl-C + rlif.on('close', bye); + rlif.on('SIGINT', bye); + + return rlif; +} + +var createWebChannel = function(transport, rlif) { + return new QWebChannel(transport, function(channel) { + // We connect to the 'sendText' signal of the remote QObject + // Be aware, that the signal is named for the remote side, + // i.e. the server wants to 'send text'. + // This can be confusing, as we connect to the signal + // to receive incoming messages on our side + channel.objects.dialog.sendText.connect(function(message) { + process.stdout.cursorTo(0); + process.stdout.clearLine(0); + console.log(' << ' + message); + rlif.prompt(); + // Go to end of existing input if any + rlif.write(null, {ctrl: true, name: 'e'}) + }); + + rlif.on('line', function(line) { + var l = line.trim(); + if (l !== '') { + process.stdout.moveCursor(0, -1); + process.stdout.cursorTo(0); + process.stdout.clearLine(0); + // The 'receiveText' slot of the remote QObject + // is called with our message. + // Again the naming is for the server side, + // i.e. the slot is used _by the server_ to receive text. + channel.objects.dialog.receiveText(l); + console.log(' >> ' + l); + } + rlif.prompt(); + }); + rlif.prompt(); + }); +} + +socket.on('open', function(event) { + console.log("info: Client connected"); + var transport = { + // We cant't do 'send: socket.send' here + // because 'send' wouldn't be bound to 'socket' + send: function(data) {socket.send(data)} + }; + + createWebChannel(transport, createReadlineInterface()); + + // QWebChannel has set up its onmessage handler + // on the transport in the constructor. + // Now we connect it to the websocket event. + socket.on('message', function(event) { + transport.onmessage(event); + }); +}); + +socket.on('error', function (error) { + console.log('Connection error: ' + error.message); + process.exit(1); +}); + +socket.on('close', function () { + console.log('Connection closed.'); + process.exit(1); +}); diff --git a/examples/webchannel/nodejs/nodejs.pro b/examples/webchannel/nodejs/nodejs.pro new file mode 100644 index 0000000..4df94d2 --- /dev/null +++ b/examples/webchannel/nodejs/nodejs.pro @@ -0,0 +1,7 @@ +TEMPLATE = aux + +exampleassets.files += \ + chatclient.js \ + package.json +exampleassets.path = $$[QT_INSTALL_EXAMPLES]/qwebchannel/nodejs +include(../exampleassets.pri) diff --git a/examples/webchannel/nodejs/package.json b/examples/webchannel/nodejs/package.json new file mode 100644 index 0000000..2d863cc --- /dev/null +++ b/examples/webchannel/nodejs/package.json @@ -0,0 +1,10 @@ +{ + "name": "Chatclient", + "description": "Chatclient example", + "author": "basysKom ", + "version": "0.0.1", + "dependencies": { + "faye-websocket": "0.7.x" + }, + "engine": "node 0.10.x" +} diff --git a/examples/webchannel/standalone/dialog.ui b/examples/webchannel/standalone/dialog.ui new file mode 100644 index 0000000..056a3f5 --- /dev/null +++ b/examples/webchannel/standalone/dialog.ui @@ -0,0 +1,48 @@ + + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + Message Contents + + + + + + + Send + + + + + + + true + + + Initializing WebChannel... + + + false + + + + + + + + diff --git a/examples/webchannel/standalone/doc/images/standalone-screenshot.png b/examples/webchannel/standalone/doc/images/standalone-screenshot.png new file mode 100644 index 0000000..3c55e19 Binary files /dev/null and b/examples/webchannel/standalone/doc/images/standalone-screenshot.png differ diff --git a/examples/webchannel/standalone/doc/src/standalone.qdoc b/examples/webchannel/standalone/doc/src/standalone.qdoc new file mode 100644 index 0000000..46c09c3 --- /dev/null +++ b/examples/webchannel/standalone/doc/src/standalone.qdoc @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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$ +** +****************************************************************************/ + +/*! + \example standalone + \title Qt WebChannel Standalone Example + \ingroup qtwebchannel-examples + \image standalone-screenshot.png + \brief Shows how to use the QWebChannel C++ API to communicate with an external client. + + The standalone example is a simple chat between a pure C++/Qt application and a remote HTML + client running in your default browser. + + \include examples-run.qdocinc + + \section1 Overview + + The C++ application sets up a QWebChannel instance and publishes a Dialog object over it. + For the remote client side, \c 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 Dialog emits the Dialog::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, Dialog::receiveText() is called. + + All communication between the HTML client and the C++/Qt server is done over a WebSocket. + The C++ side instantiates a QWebSocketServer and wraps incoming QWebSocket connections + in WebSocketTransport objects, which implement QWebChannelAbstractTransport. These objects are + then connected to the QWebChannel instance. +*/ diff --git a/examples/webchannel/standalone/index.html b/examples/webchannel/standalone/index.html new file mode 100644 index 0000000..778a502 --- /dev/null +++ b/examples/webchannel/standalone/index.html @@ -0,0 +1,79 @@ + + + + + + + + + +
+ + + diff --git a/examples/webchannel/standalone/main.cpp b/examples/webchannel/standalone/main.cpp new file mode 100644 index 0000000..2cbc6b8 --- /dev/null +++ b/examples/webchannel/standalone/main.cpp @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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 "qwebchannel.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include "websocketclientwrapper.h" +#include "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; +}; + +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + + // setup the QWebSocketServer + QWebSocketServer server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode); + if (!server.listen(QHostAddress::LocalHost, 12345)) { + qFatal("Failed to open web socket server."); + return 1; + } + + // wrap WebSocket clients in QWebChannelAbstractTransport objects + WebSocketClientWrapper clientWrapper(&server); + + // setup the channel + QWebChannel channel; + QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected, + &channel, &QWebChannel::connectTo); + + // setup the dialog and publish it to the QWebChannel + Dialog dialog; + channel.registerObject(QStringLiteral("dialog"), &dialog); + + // open a browser window with the client HTML page + QUrl url = QUrl::fromLocalFile(BUILD_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(); +} + +#include "main.moc" diff --git a/examples/webchannel/standalone/standalone.pro b/examples/webchannel/standalone/standalone.pro new file mode 100644 index 0000000..fdeb87a --- /dev/null +++ b/examples/webchannel/standalone/standalone.pro @@ -0,0 +1,22 @@ +QT += gui webchannel widgets websockets + +CONFIG += warn_on + +SOURCES += \ + main.cpp \ + websockettransport.cpp \ + websocketclientwrapper.cpp + +HEADERS += \ + websockettransport.h \ + websocketclientwrapper.h + +FORMS += \ + dialog.ui + +DEFINES += "BUILD_DIR=\"\\\""$$OUT_PWD"\\\"\"" + +exampleassets.files += \ + index.html +exampleassets.path = $$[QT_INSTALL_EXAMPLES]/qwebchannel/standalone +include(../exampleassets.pri) diff --git a/examples/webchannel/standalone/websocketclientwrapper.cpp b/examples/webchannel/standalone/websocketclientwrapper.cpp new file mode 100644 index 0000000..0e776e1 --- /dev/null +++ b/examples/webchannel/standalone/websocketclientwrapper.cpp @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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 + +#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 + +/*! + Construct the client wrapper with the given parent. + + All clients connecting to the QWebSocketServer will be automatically wrapped + in WebSocketTransport objects. +*/ +WebSocketClientWrapper::WebSocketClientWrapper(QWebSocketServer *server, QObject *parent) + : QObject(parent) + , m_server(server) +{ + connect(server, &QWebSocketServer::newConnection, + this, &WebSocketClientWrapper::handleNewConnection); +} + +/*! + Wrap an incoming WebSocket connection in a WebSocketTransport object. +*/ +void WebSocketClientWrapper::handleNewConnection() +{ + emit clientConnected(new WebSocketTransport(m_server->nextPendingConnection())); +} + +QT_END_NAMESPACE diff --git a/examples/webchannel/standalone/websocketclientwrapper.h b/examples/webchannel/standalone/websocketclientwrapper.h new file mode 100644 index 0000000..1f742f7 --- /dev/null +++ b/examples/webchannel/standalone/websocketclientwrapper.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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 + +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/webchannel/standalone/websockettransport.cpp b/examples/webchannel/standalone/websockettransport.cpp new file mode 100644 index 0000000..5714463 --- /dev/null +++ b/examples/webchannel/standalone/websockettransport.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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 +#include +#include + +#include + +/*! + \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 + +/*! + Construct the transport object and wrap the given socket. + + The socket is also set as the parent of the transport object. +*/ +WebSocketTransport::WebSocketTransport(QWebSocket *socket) +: QWebChannelAbstractTransport(socket) +, m_socket(socket) +{ + connect(socket, &QWebSocket::textMessageReceived, + this, &WebSocketTransport::textMessageReceived); +} + +/*! + Destroys the WebSocketTransport. +*/ +WebSocketTransport::~WebSocketTransport() +{ + +} + +/*! + Serialize the JSON message and send it as a text message via the WebSocket to the client. +*/ +void WebSocketTransport::sendMessage(const QJsonObject &message) +{ + QJsonDocument doc(message); + m_socket->sendTextMessage(QString::fromUtf8(doc.toJson(QJsonDocument::Compact))); +} + +/*! + Deserialize the stringified JSON messageData and emit messageReceived. +*/ +void WebSocketTransport::textMessageReceived(const QString &messageData) +{ + 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/webchannel/standalone/websockettransport.h b/examples/webchannel/standalone/websockettransport.h new file mode 100644 index 0000000..828ac00 --- /dev/null +++ b/examples/webchannel/standalone/websockettransport.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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 + +QT_BEGIN_NAMESPACE + +class QWebSocket; +class WebSocketTransport : public QWebChannelAbstractTransport +{ + Q_OBJECT +public: + explicit WebSocketTransport(QWebSocket *socket); + virtual ~WebSocketTransport(); + + void sendMessage(const QJsonObject &message) Q_DECL_OVERRIDE; + +private Q_SLOTS: + void textMessageReceived(const QString &message); + +private: + QWebSocket *m_socket; +}; + +QT_END_NAMESPACE + +#endif // WEBSOCKETTRANSPORT_H diff --git a/examples/webchannel/webchannel.pro b/examples/webchannel/webchannel.pro new file mode 100644 index 0000000..de6b4b8 --- /dev/null +++ b/examples/webchannel/webchannel.pro @@ -0,0 +1,7 @@ +TEMPLATE = subdirs + +qtHaveModule(widgets):qtHaveModule(websockets) { + SUBDIRS += standalone +} + +SUBDIRS += nodejs diff --git a/src/webchannel/doc/src/examples.qdoc b/src/webchannel/doc/src/examples.qdoc new file mode 100644 index 0000000..613d760 --- /dev/null +++ b/src/webchannel/doc/src/examples.qdoc @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff +** 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$ +** +****************************************************************************/ + +/*! + \title Qt WebChannel Examples + \group qtwebchannel-examples + \brief List of Qt WebChannel examples. + + The examples show how to use the QWebChannel C++ and WebChannel QML API. + +*/ diff --git a/src/webchannel/doc/src/javascript.qdoc b/src/webchannel/doc/src/javascript.qdoc index de211d0..fa8edfe 100644 --- a/src/webchannel/doc/src/javascript.qdoc +++ b/src/webchannel/doc/src/javascript.qdoc @@ -49,7 +49,8 @@ Note that the JavaScript QWebChannel object should be constructed once the transport object is fully operational. In case of a WebSocket, that means you should create the QWebChannel in the - socket's \c onopen handler. Take a look at the \l{Standalone Example} to see how this is done. + socket's \c onopen handler. Take a look at the \l{Qt WebChannel Standalone Example} to see how + this is done. \section1 Interacting with QObjects diff --git a/src/webchannel/qwebchannel.cpp b/src/webchannel/qwebchannel.cpp index 99becfb..e3df0b9 100644 --- a/src/webchannel/qwebchannel.cpp +++ b/src/webchannel/qwebchannel.cpp @@ -73,7 +73,7 @@ QT_BEGIN_NAMESPACE There also exists a declarative WebChannel API. - \sa {Standalone Example} + \sa {Qt WebChannel Standalone Example} */ /*! diff --git a/src/webchannel/qwebchannelabstracttransport.cpp b/src/webchannel/qwebchannelabstracttransport.cpp index f261698..0bfdbb6 100644 --- a/src/webchannel/qwebchannelabstracttransport.cpp +++ b/src/webchannel/qwebchannelabstracttransport.cpp @@ -52,13 +52,13 @@ QT_BEGIN_NAMESPACE Users of the QWebChannel must implement this interface and connect instances of it to the QWebChannel server for every client that should be connected to the QWebChannel. - The {Standalone Example} shows how this can be done using Qt WebSockets. Qt WebKit implements - this interface internally and uses the native WebKit IPC mechanism to transmit messages - to HTML clients. + The {Qt WebChannel Standalone Example}{Standalone Example} shows how this can be done + using Qt WebSockets. Qt WebKit implements this interface internally and uses the native + WebKit IPC mechanism to transmit messages to HTML clients. \note The JSON message protocol is considered internal and might change over time. - \sa {Standalone Example} + \sa {Qt WebChannel Standalone Example} */ /*! -- cgit v1.2.3