summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@digia.com>2014-08-06 16:00:50 +0200
committerSumedha Widyadharma <sumedha.widyadharma@basyskom.com>2014-08-07 15:05:39 +0200
commitcb01c71502898666d286e42c8e552b78b854fc2b (patch)
treeee9a88c89a842533e531f527c612ee6d3195b72d /examples
parent4ce1fd950d8c20d21c851340350fb2b1dc920ce4 (diff)
downloadqtwebchannel-cb01c71502898666d286e42c8e552b78b854fc2b.tar.gz
Added a nodeJS example
This is a small sample of a command line chat client which shows how to use QWebChannel with NodeJS. See included readme for instructions on how to execute it. A known port has been introduced to make this work. Change-Id: If430703f8c2267df758df9d5978509675c8aea9d Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/qwebchannel/nodejs/README14
-rw-r--r--examples/qwebchannel/nodejs/chatclient.js133
-rw-r--r--examples/qwebchannel/nodejs/nodejs.pro7
-rw-r--r--examples/qwebchannel/nodejs/package.json10
-rw-r--r--examples/qwebchannel/qwebchannel.pro2
-rw-r--r--examples/qwebchannel/standalone/main.cpp2
6 files changed, 167 insertions, 1 deletions
diff --git a/examples/qwebchannel/nodejs/README b/examples/qwebchannel/nodejs/README
new file mode 100644
index 0000000..07f8fd8
--- /dev/null
+++ b/examples/qwebchannel/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/qwebchannel/nodejs/chatclient.js b/examples/qwebchannel/nodejs/chatclient.js
new file mode 100644
index 0000000..03ce214
--- /dev/null
+++ b/examples/qwebchannel/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 <bernd.lamecker@basyskom.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$
+**
+****************************************************************************/
+'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
new file mode 100644
index 0000000..4df94d2
--- /dev/null
+++ b/examples/qwebchannel/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/qwebchannel/nodejs/package.json b/examples/qwebchannel/nodejs/package.json
new file mode 100644
index 0000000..2d863cc
--- /dev/null
+++ b/examples/qwebchannel/nodejs/package.json
@@ -0,0 +1,10 @@
+{
+ "name": "Chatclient",
+ "description": "Chatclient example",
+ "author": "basysKom <info@basyskom.com>",
+ "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
index 63677f5..de6b4b8 100644
--- a/examples/qwebchannel/qwebchannel.pro
+++ b/examples/qwebchannel/qwebchannel.pro
@@ -3,3 +3,5 @@ TEMPLATE = subdirs
qtHaveModule(widgets):qtHaveModule(websockets) {
SUBDIRS += standalone
}
+
+SUBDIRS += nodejs
diff --git a/examples/qwebchannel/standalone/main.cpp b/examples/qwebchannel/standalone/main.cpp
index fa3912d..2cbc6b8 100644
--- a/examples/qwebchannel/standalone/main.cpp
+++ b/examples/qwebchannel/standalone/main.cpp
@@ -121,7 +121,7 @@ int main(int argc, char** argv)
// setup the QWebSocketServer
QWebSocketServer server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode);
- if (!server.listen(QHostAddress::LocalHost)) {
+ if (!server.listen(QHostAddress::LocalHost, 12345)) {
qFatal("Failed to open web socket server.");
return 1;
}