diff options
Diffstat (limited to 'examples/websockets')
37 files changed, 2103 insertions, 0 deletions
diff --git a/examples/websockets/doc/echoclient.qdoc b/examples/websockets/doc/echoclient.qdoc new file mode 100644 index 0000000..a6cb1a9 --- /dev/null +++ b/examples/websockets/doc/echoclient.qdoc @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 echoclient + \title Echo Client Example + \ingroup qtwebsockets-examples + \brief Describes how to use the websocket API for creating a simple echo client. + + The Echo Client Example shows how to use the websocket API to create a simple + echo client. + + \sa {Echo Server Example} +*/ diff --git a/examples/websockets/doc/echoserver.qdoc b/examples/websockets/doc/echoserver.qdoc new file mode 100644 index 0000000..fcb0442 --- /dev/null +++ b/examples/websockets/doc/echoserver.qdoc @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 echoserver + \title Echo Server Example + \ingroup qtwebsockets-examples + \brief Shows how to create a simple server application that + sends back the messages it receives. + + The Echo Server Example shows how to create a simple server application that + sends back the messages it receives, using the websockets API. + + \sa {Echo Client Example} +*/ diff --git a/examples/websockets/doc/examples.qdoc b/examples/websockets/doc/examples.qdoc new file mode 100644 index 0000000..a9cc739 --- /dev/null +++ b/examples/websockets/doc/examples.qdoc @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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$ +** +****************************************************************************/ + + +/*! + \group qtwebsockets-examples + \title Qt WebSockets Examples + \brief List of Qt WebSocket examples + + The examples below can be used as a guide to using the \l{Qt WebSockets} API. +*/ diff --git a/examples/websockets/doc/qmlwebsocketclient.qdoc b/examples/websockets/doc/qmlwebsocketclient.qdoc new file mode 100644 index 0000000..776e486 --- /dev/null +++ b/examples/websockets/doc/qmlwebsocketclient.qdoc @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 qmlwebsocketclient + \title QML WebSocket Client Example + \ingroup qtwebsockets-examples + \brief Explains how to program a QML WebSocket client example. +*/ diff --git a/examples/websockets/doc/simplechat.qdoc b/examples/websockets/doc/simplechat.qdoc new file mode 100644 index 0000000..506d45a --- /dev/null +++ b/examples/websockets/doc/simplechat.qdoc @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 simplechat + \title Simple Chat Example + \ingroup qtwebsockets-examples + \brief Shows how to use the QWebSocket and QWebSocketServer classes + for creating a minimalistic chat application over websockets. + + The Simple Chat Example shows how to use the QWebSocket and QWebSocketServer + classes to create a minimalistic chat application over websockets. + +*/ diff --git a/examples/websockets/doc/sslechoclient.qdoc b/examples/websockets/doc/sslechoclient.qdoc new file mode 100644 index 0000000..d7d1897 --- /dev/null +++ b/examples/websockets/doc/sslechoclient.qdoc @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 sslechoclient + \title SSL Echo Client Example + \ingroup qtwebsockets-examples + \brief Shows how to use the QWebSocket class to implement an echo + client over a secure connection (wss). + + The SSL Echo Client example shows how to use the QWebSocket class to implement + an echo client over a secure connection (wss). + + \sa {Echo Client Example}, {SSL Echo Server Example} +*/ diff --git a/examples/websockets/doc/sslechoserver.qdoc b/examples/websockets/doc/sslechoserver.qdoc new file mode 100644 index 0000000..d158a68 --- /dev/null +++ b/examples/websockets/doc/sslechoserver.qdoc @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 sslechoserver + \title SSL Echo Server Example + \ingroup qtwebsockets-examples + \brief Shows how to use the QWebSocketServer class for implementing a simple + echo server over secure sockets (wss). + + The SSL Echo Server example shows how to use the QWebSocketServer class + to implement a simple echo server over secure sockets (wss). + + \sa {SSL Echo Client Example}, {Echo Server Example} +*/ diff --git a/examples/websockets/echoclient/echoclient.cpp b/examples/websockets/echoclient/echoclient.cpp new file mode 100644 index 0000000..9e8ba9b --- /dev/null +++ b/examples/websockets/echoclient/echoclient.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 "echoclient.h" +#include <QtCore/QDebug> + +QT_USE_NAMESPACE + +//! [constructor] +EchoClient::EchoClient(const QUrl &url, QObject *parent) : + QObject(parent), + m_url(url) +{ + connect(&m_webSocket, &QWebSocket::connected, this, &EchoClient::onConnected); + connect(&m_webSocket, &QWebSocket::disconnected, this, &EchoClient::closed); + m_webSocket.open(QUrl(url)); +} +//! [constructor] + +//! [onConnected] +void EchoClient::onConnected() +{ + qDebug() << "Websocket connected"; + connect(&m_webSocket, &QWebSocket::textMessageReceived, + this, &EchoClient::onTextMessageReceived); + m_webSocket.sendTextMessage(QStringLiteral("Hello, world!")); +} +//! [onConnected] + +//! [onTextMessageReceived] +void EchoClient::onTextMessageReceived(QString message) +{ + qDebug() << "Message received:" << message; + m_webSocket.close(); +} +//! [onTextMessageReceived] diff --git a/examples/websockets/echoclient/echoclient.h b/examples/websockets/echoclient/echoclient.h new file mode 100644 index 0000000..b5a7709 --- /dev/null +++ b/examples/websockets/echoclient/echoclient.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 ECHOCLIENT_H +#define ECHOCLIENT_H + +#include <QtCore/QObject> +#include <QtWebSockets/QWebSocket> + +class EchoClient : public QObject +{ + Q_OBJECT +public: + explicit EchoClient(const QUrl &url, QObject *parent = Q_NULLPTR); + +Q_SIGNALS: + void closed(); + +private Q_SLOTS: + void onConnected(); + void onTextMessageReceived(QString message); + +private: + QWebSocket m_webSocket; + QUrl m_url; +}; + +#endif // ECHOCLIENT_H diff --git a/examples/websockets/echoclient/echoclient.pro b/examples/websockets/echoclient/echoclient.pro new file mode 100644 index 0000000..24848e7 --- /dev/null +++ b/examples/websockets/echoclient/echoclient.pro @@ -0,0 +1,15 @@ +QT += core websockets +QT -= gui + +TARGET = echoclient +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += \ + main.cpp \ + echoclient.cpp + +HEADERS += \ + echoclient.h diff --git a/examples/websockets/echoclient/main.cpp b/examples/websockets/echoclient/main.cpp new file mode 100644 index 0000000..fccaed1 --- /dev/null +++ b/examples/websockets/echoclient/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 <QtCore/QCoreApplication> +#include "echoclient.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + EchoClient client(QUrl(QStringLiteral("ws://localhost:1234"))); + QObject::connect(&client, &EchoClient::closed, &a, &QCoreApplication::quit); + + return a.exec(); +} diff --git a/examples/websockets/echoserver/echoclient.html b/examples/websockets/echoserver/echoclient.html new file mode 100644 index 0000000..e9c55dc --- /dev/null +++ b/examples/websockets/echoserver/echoclient.html @@ -0,0 +1,102 @@ +<html> + <head> + <title>Websocket Echo Client</title> + </head> + <body> + <h1>Websocket Echo Client</h1> + <p> + <button onClick="initWebsocket();">Connect</button> + <button onClick="stopWebsocket();">Disconnect</button> + <button onClick="checkSocket();">State</button> + </p> + <p> + <textarea id="debugTextArea" style="width:400px;height:200px;"></textarea> + </p> + <p> + <input type="text" id="inputText" onkeydown="if(event.keyCode==13)sendMessage();"/> + <button onClick="sendMessage();">Send</button> + </p> + + <script type="text/javascript"> + var debugTextArea = document.getElementById("debugTextArea"); + function debug(message) { + debugTextArea.value += message + "\n"; + debugTextArea.scrollTop = debugTextArea.scrollHeight; + } + + function sendMessage() { + var msg = document.getElementById("inputText").value; + if ( websocket != null ) + { + document.getElementById("inputText").value = ""; + websocket.send( msg ); + console.log( "string sent :", '"'+msg+'"' ); + } + } + + var wsUri = "ws://localhost:1234"; + var websocket = null; + + function initWebsocket() { + try { + if (typeof MozWebSocket == 'function') + WebSocket = MozWebSocket; + if ( websocket && websocket.readyState == 1 ) + websocket.close(); + websocket = new WebSocket( wsUri ); + websocket.onopen = function (evt) { + debug("CONNECTED"); + }; + websocket.onclose = function (evt) { + debug("DISCONNECTED"); + }; + websocket.onmessage = function (evt) { + console.log( "Message received :", evt.data ); + debug( evt.data ); + }; + websocket.onerror = function (evt) { + debug('ERROR: ' + evt.data); + }; + } catch (exception) { + debug('ERROR: ' + exception); + } + } + + function stopWebsocket() { + if (websocket) + websocket.close(); + } + + function checkSocket() { + if (websocket != null) { + var stateStr; + switch (websocket.readyState) { + case 0: { + stateStr = "CONNECTING"; + break; + } + case 1: { + stateStr = "OPEN"; + break; + } + case 2: { + stateStr = "CLOSING"; + break; + } + case 3: { + stateStr = "CLOSED"; + break; + } + default: { + stateStr = "UNKNOW"; + break; + } + } + debug("Websocket state = " + websocket.readyState + " ( " + stateStr + " )"); + } else { + debug("Websocket is null"); + } + } + </script> + </body> +</html> diff --git a/examples/websockets/echoserver/echoserver.cpp b/examples/websockets/echoserver/echoserver.cpp new file mode 100644 index 0000000..b35df53 --- /dev/null +++ b/examples/websockets/echoserver/echoserver.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 "echoserver.h" +#include "QtWebSockets/qwebsocketserver.h" +#include "QtWebSockets/qwebsocket.h" +#include <QtCore/QDebug> + +QT_USE_NAMESPACE + +//! [constructor] +EchoServer::EchoServer(quint16 port, QObject *parent) : + QObject(parent), + m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"), + QWebSocketServer::NonSecureMode, this)), + m_clients() +{ + if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { + qDebug() << "Echoserver listening on port" << port; + connect(m_pWebSocketServer, &QWebSocketServer::newConnection, + this, &EchoServer::onNewConnection); + connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &EchoServer::closed); + } +} +//! [constructor] + +EchoServer::~EchoServer() +{ + m_pWebSocketServer->close(); + qDeleteAll(m_clients.begin(), m_clients.end()); +} + +//! [onNewConnection] +void EchoServer::onNewConnection() +{ + QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); + + connect(pSocket, &QWebSocket::textMessageReceived, this, &EchoServer::processTextMessage); + connect(pSocket, &QWebSocket::binaryMessageReceived, this, &EchoServer::processBinaryMessage); + connect(pSocket, &QWebSocket::disconnected, this, &EchoServer::socketDisconnected); + + m_clients << pSocket; +} +//! [onNewConnection] + +//! [processTextMessage] +void EchoServer::processTextMessage(QString message) +{ + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) { + pClient->sendTextMessage(message); + } + m_pWebSocketServer->close(); +} +//! [processTextMessage] + +//! [processBinaryMessage] +void EchoServer::processBinaryMessage(QByteArray message) +{ + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) { + pClient->sendBinaryMessage(message); + } +} +//! [processBinaryMessage] + +//! [socketDisconnected] +void EchoServer::socketDisconnected() +{ + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) { + m_clients.removeAll(pClient); + pClient->deleteLater(); + } +} +//! [socketDisconnected] diff --git a/examples/websockets/echoserver/echoserver.h b/examples/websockets/echoserver/echoserver.h new file mode 100644 index 0000000..521eccb --- /dev/null +++ b/examples/websockets/echoserver/echoserver.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 ECHOSERVER_H +#define ECHOSERVER_H + +#include <QtCore/QObject> +#include <QtCore/QList> +#include <QtCore/QByteArray> + +QT_FORWARD_DECLARE_CLASS(QWebSocketServer) +QT_FORWARD_DECLARE_CLASS(QWebSocket) + +class EchoServer : public QObject +{ + Q_OBJECT +public: + explicit EchoServer(quint16 port, QObject *parent = Q_NULLPTR); + ~EchoServer(); + +Q_SIGNALS: + void closed(); + +private Q_SLOTS: + void onNewConnection(); + void processTextMessage(QString message); + void processBinaryMessage(QByteArray message); + void socketDisconnected(); + +private: + QWebSocketServer *m_pWebSocketServer; + QList<QWebSocket *> m_clients; +}; + +#endif //ECHOSERVER_H diff --git a/examples/websockets/echoserver/echoserver.pro b/examples/websockets/echoserver/echoserver.pro new file mode 100644 index 0000000..6d2360b --- /dev/null +++ b/examples/websockets/echoserver/echoserver.pro @@ -0,0 +1,17 @@ +QT += core websockets +QT -= gui + +TARGET = echoserver +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += \ + main.cpp \ + echoserver.cpp + +HEADERS += \ + echoserver.h + +OTHER_FILES += echoclient.html diff --git a/examples/websockets/echoserver/main.cpp b/examples/websockets/echoserver/main.cpp new file mode 100644 index 0000000..ff3993e --- /dev/null +++ b/examples/websockets/echoserver/main.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 <QtCore/QCoreApplication> +#include "echoserver.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + EchoServer *server = new EchoServer(1234); + QObject::connect(server, &EchoServer::closed, &a, &QCoreApplication::quit); + + return a.exec(); +} diff --git a/examples/websockets/qmlwebsocketclient/data.qrc b/examples/websockets/qmlwebsocketclient/data.qrc new file mode 100644 index 0000000..3ac4604 --- /dev/null +++ b/examples/websockets/qmlwebsocketclient/data.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>qml/qmlwebsocketclient/main.qml</file> + </qresource> +</RCC> diff --git a/examples/websockets/qmlwebsocketclient/main.cpp b/examples/websockets/qmlwebsocketclient/main.cpp new file mode 100644 index 0000000..3aa1c95 --- /dev/null +++ b/examples/websockets/qmlwebsocketclient/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 <QtGui/QGuiApplication> +#include <QQuickView> + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + QQuickView view; + view.setSource(QUrl(QStringLiteral("qrc:/qml/qmlwebsocketclient/main.qml"))); + view.show(); + + return app.exec(); +} diff --git a/examples/websockets/qmlwebsocketclient/qml/qmlwebsocketclient/main.qml b/examples/websockets/qmlwebsocketclient/qml/qmlwebsocketclient/main.qml new file mode 100644 index 0000000..5560c4c --- /dev/null +++ b/examples/websockets/qmlwebsocketclient/qml/qmlwebsocketclient/main.qml @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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$ +** +****************************************************************************/ +import QtQuick 2.0 +import Qt.WebSockets 1.0 + +Rectangle { + width: 360 + height: 360 + + WebSocket { + id: socket + url: "ws://echo.websocket.org" + onTextMessageReceived: { + messageBox.text = messageBox.text + "\nReceived message: " + message + } + onStatusChanged: if (socket.status == WebSocket.Error) { + console.log("Error: " + socket.errorString) + } else if (socket.status == WebSocket.Open) { + socket.sendTextMessage("Hello World") + } else if (socket.status == WebSocket.Closed) { + messageBox.text += "\nSocket closed" + } + active: false + } + + WebSocket { + id: secureWebSocket + url: "wss://echo.websocket.org" + onTextMessageReceived: { + messageBox.text = messageBox.text + "\nReceived secure message: " + message + } + onStatusChanged: if (secureWebSocket.status == WebSocket.Error) { + console.log("Error: " + secureWebSocket.errorString) + } else if (secureWebSocket.status == WebSocket.Open) { + secureWebSocket.sendTextMessage("Hello Secure World") + } else if (secureWebSocket.status == WebSocket.Closed) { + messageBox.text += "\nSecure socket closed" + } + active: false + } + Text { + id: messageBox + text: socket.status == WebSocket.Open ? qsTr("Sending...") : qsTr("Welcome!") + anchors.centerIn: parent + } + + MouseArea { + anchors.fill: parent + onClicked: { + socket.active = !socket.active + secureWebSocket.active = !secureWebSocket.active; + //Qt.quit(); + } + } +} diff --git a/examples/websockets/qmlwebsocketclient/qmlwebsocketclient.pro b/examples/websockets/qmlwebsocketclient/qmlwebsocketclient.pro new file mode 100644 index 0000000..e7dc82a --- /dev/null +++ b/examples/websockets/qmlwebsocketclient/qmlwebsocketclient.pro @@ -0,0 +1,13 @@ +QT += core qml quick websockets + +TARGET = qmlwebsocketclient + +TEMPLATE = app + +CONFIG -= app_bundle + +SOURCES += main.cpp + +RESOURCES += data.qrc + +OTHER_FILES += qml/qmlwebsocketclient/main.qml diff --git a/examples/websockets/simplechat/chatclient.html b/examples/websockets/simplechat/chatclient.html new file mode 100644 index 0000000..5b077f7 --- /dev/null +++ b/examples/websockets/simplechat/chatclient.html @@ -0,0 +1,106 @@ +<html> + <head> + <title>Websocket Chat Client</title> + </head> + <body> + <h1>Websocket Chat Client</h1> + <p> + <button onClick="initWebsocket();">Connect</button> + <button onClick="stopWebsocket();">Disconnect</button> + <button onClick="checkSocket();">State</button> + </p> + <p> + <textarea id="debugTextArea" style="width:400px;height:200px;"></textarea> + </p> + <p> + <input type="text" id="inputNick" value="nickname" /> + <input type="text" id="inputText" onkeydown="if(event.keyCode==13)sendMessage();"/> + <button onClick="sendMessage();">Send</button> + </p> + + <script type="text/javascript"> + var debugTextArea = document.getElementById("debugTextArea"); + function debug(message) { + debugTextArea.value += message + "\n"; + debugTextArea.scrollTop = debugTextArea.scrollHeight; + } + + function sendMessage() { + var nickname = document.getElementById("inputNick").value; + var msg = document.getElementById("inputText").value; + var strToSend = nickname + ": " + msg; + if ( websocket != null ) + { + document.getElementById("inputText").value = ""; + websocket.send( strToSend ); + console.log( "string sent :", '"'+strToSend+'"' ); + debug(strToSend); + } + } + + var wsUri = "ws://localhost:1234"; + var websocket = null; + + function initWebsocket() { + try { + if (typeof MozWebSocket == 'function') + WebSocket = MozWebSocket; + if ( websocket && websocket.readyState == 1 ) + websocket.close(); + websocket = new WebSocket( wsUri ); + websocket.onopen = function (evt) { + debug("CONNECTED"); + }; + websocket.onclose = function (evt) { + debug("DISCONNECTED"); + }; + websocket.onmessage = function (evt) { + console.log( "Message received :", evt.data ); + debug( evt.data ); + }; + websocket.onerror = function (evt) { + debug('ERROR: ' + evt.data); + }; + } catch (exception) { + debug('ERROR: ' + exception); + } + } + + function stopWebsocket() { + if (websocket) + websocket.close(); + } + + function checkSocket() { + if (websocket != null) { + var stateStr; + switch (websocket.readyState) { + case 0: { + stateStr = "CONNECTING"; + break; + } + case 1: { + stateStr = "OPEN"; + break; + } + case 2: { + stateStr = "CLOSING"; + break; + } + case 3: { + stateStr = "CLOSED"; + break; + } + default: { + stateStr = "UNKNOW"; + break; + } + } + debug("Websocket state = " + websocket.readyState + " ( " + stateStr + " )"); + } else { + debug("Websocket is null"); + } + } + </script> + </body> +</html> diff --git a/examples/websockets/simplechat/chatserver.cpp b/examples/websockets/simplechat/chatserver.cpp new file mode 100644 index 0000000..ba5c97c --- /dev/null +++ b/examples/websockets/simplechat/chatserver.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 "chatserver.h" +#include "QtWebSockets/QWebSocketServer" +#include "QtWebSockets/QWebSocket" +#include <QtCore/QDebug> + +QT_USE_NAMESPACE + +//! [constructor] +ChatServer::ChatServer(quint16 port, QObject *parent) : + QObject(parent), + m_pWebSocketServer(Q_NULLPTR), + m_clients() +{ + m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Chat Server"), + QWebSocketServer::NonSecureMode, + this); + if (m_pWebSocketServer->listen(QHostAddress::Any, port)) + { + qDebug() << "Chat Server listening on port" << port; + connect(m_pWebSocketServer, &QWebSocketServer::newConnection, + this, &ChatServer::onNewConnection); + } +} + +ChatServer::~ChatServer() +{ + m_pWebSocketServer->close(); + qDeleteAll(m_clients.begin(), m_clients.end()); +} +//! [constructor] + +//! [onNewConnection] +void ChatServer::onNewConnection() +{ + QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); + + connect(pSocket, &QWebSocket::textMessageReceived, this, &ChatServer::processMessage); + connect(pSocket, &QWebSocket::disconnected, this, &ChatServer::socketDisconnected); + + m_clients << pSocket; +} +//! [onNewConnection] + +//! [processMessage] +void ChatServer::processMessage(QString message) +{ + QWebSocket *pSender = qobject_cast<QWebSocket *>(sender()); + Q_FOREACH (QWebSocket *pClient, m_clients) + { + if (pClient != pSender) //don't echo message back to sender + { + pClient->sendTextMessage(message); + } + } +} +//! [processMessage] + +//! [socketDisconnected] +void ChatServer::socketDisconnected() +{ + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) + { + m_clients.removeAll(pClient); + pClient->deleteLater(); + } +} +//! [socketDisconnected] diff --git a/examples/websockets/simplechat/chatserver.h b/examples/websockets/simplechat/chatserver.h new file mode 100644 index 0000000..41dad41 --- /dev/null +++ b/examples/websockets/simplechat/chatserver.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 CHATSERVER_H +#define CHATSERVER_H + +#include <QtCore/QObject> +#include <QtCore/QList> +#include <QtCore/QByteArray> + +QT_FORWARD_DECLARE_CLASS(QWebSocketServer) +QT_FORWARD_DECLARE_CLASS(QWebSocket) + +class ChatServer : public QObject +{ + Q_OBJECT +public: + explicit ChatServer(quint16 port, QObject *parent = Q_NULLPTR); + virtual ~ChatServer(); + +private Q_SLOTS: + void onNewConnection(); + void processMessage(QString message); + void socketDisconnected(); + +private: + QWebSocketServer *m_pWebSocketServer; + QList<QWebSocket *> m_clients; +}; + +#endif //CHATSERVER_H diff --git a/examples/websockets/simplechat/main.cpp b/examples/websockets/simplechat/main.cpp new file mode 100644 index 0000000..a44f16e --- /dev/null +++ b/examples/websockets/simplechat/main.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 <QtCore/QCoreApplication> +#include "chatserver.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + ChatServer server(1234); + + return a.exec(); +} diff --git a/examples/websockets/simplechat/simplechat.pro b/examples/websockets/simplechat/simplechat.pro new file mode 100644 index 0000000..5cb6d4f --- /dev/null +++ b/examples/websockets/simplechat/simplechat.pro @@ -0,0 +1,17 @@ +QT += core websockets +QT -= gui + +TARGET = chatserver +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += \ + main.cpp \ + chatserver.cpp + +HEADERS += \ + chatserver.h + +OTHER_FILES += *.html diff --git a/examples/websockets/sslechoclient/main.cpp b/examples/websockets/sslechoclient/main.cpp new file mode 100644 index 0000000..228b37d --- /dev/null +++ b/examples/websockets/sslechoclient/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 <QtCore/QCoreApplication> +#include "sslechoclient.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + SslEchoClient client(QUrl(QStringLiteral("wss://localhost:1234"))); + + Q_UNUSED(client); + + return a.exec(); +} diff --git a/examples/websockets/sslechoclient/sslechoclient.cpp b/examples/websockets/sslechoclient/sslechoclient.cpp new file mode 100644 index 0000000..037990d --- /dev/null +++ b/examples/websockets/sslechoclient/sslechoclient.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 "sslechoclient.h" +#include <QtCore/QDebug> +#include <QtWebSockets/QWebSocket> +#include <QCoreApplication> + +QT_USE_NAMESPACE + +//! [constructor] +SslEchoClient::SslEchoClient(const QUrl &url, QObject *parent) : + QObject(parent), + m_webSocket() +{ + connect(&m_webSocket, &QWebSocket::connected, this, &SslEchoClient::onConnected); + typedef void (QWebSocket:: *sslErrorsSignal)(const QList<QSslError> &); + connect(&m_webSocket, static_cast<sslErrorsSignal>(&QWebSocket::sslErrors), + this, &SslEchoClient::onSslErrors); + m_webSocket.open(QUrl(url)); +} +//! [constructor] + +//! [onConnected] +void SslEchoClient::onConnected() +{ + qDebug() << "Websocket connected"; + connect(&m_webSocket, &QWebSocket::textMessageReceived, + this, &SslEchoClient::onTextMessageReceived); + m_webSocket.sendTextMessage(QStringLiteral("Hello, world!")); +} +//! [onConnected] + +//! [onTextMessageReceived] +void SslEchoClient::onTextMessageReceived(QString message) +{ + qDebug() << "Message received:" << message; + qApp->quit(); +} + +void SslEchoClient::onSslErrors(const QList<QSslError> &errors) +{ + Q_UNUSED(errors); + + // WARNING: Never ignore SSL errors in production code. + // The proper way to handle self-signed certificates is to add a custom root + // to the CA store. + + m_webSocket.ignoreSslErrors(); +} +//! [onTextMessageReceived] diff --git a/examples/websockets/sslechoclient/sslechoclient.h b/examples/websockets/sslechoclient/sslechoclient.h new file mode 100644 index 0000000..7ec373b --- /dev/null +++ b/examples/websockets/sslechoclient/sslechoclient.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 SSLECHOCLIENT_H +#define SSLECHOCLIENT_H + +#include <QtCore/QObject> +#include <QtWebSockets/QWebSocket> +#include <QtNetwork/QSslError> +#include <QtCore/QList> +#include <QtCore/QString> +#include <QtCore/QUrl> + +QT_FORWARD_DECLARE_CLASS(QWebSocket) + +class SslEchoClient : public QObject +{ + Q_OBJECT +public: + explicit SslEchoClient(const QUrl &url, QObject *parent = Q_NULLPTR); + +private Q_SLOTS: + void onConnected(); + void onTextMessageReceived(QString message); + void onSslErrors(const QList<QSslError> &errors); + +private: + QWebSocket m_webSocket; +}; + +#endif // SSLECHOCLIENT_H diff --git a/examples/websockets/sslechoclient/sslechoclient.pro b/examples/websockets/sslechoclient/sslechoclient.pro new file mode 100644 index 0000000..431301f --- /dev/null +++ b/examples/websockets/sslechoclient/sslechoclient.pro @@ -0,0 +1,15 @@ +QT += core websockets +QT -= gui + +TARGET = sslechoclient +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += \ + main.cpp \ + sslechoclient.cpp + +HEADERS += \ + sslechoclient.h diff --git a/examples/websockets/sslechoserver/localhost.cert b/examples/websockets/sslechoserver/localhost.cert new file mode 100644 index 0000000..7aa33a1 --- /dev/null +++ b/examples/websockets/sslechoserver/localhost.cert @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC+zCCAeOgAwIBAgIJAP26rumH9qOkMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV +BAMMCWxvY2FsaG9zdDAeFw0xMzExMDYxNjU4NTRaFw0yMzExMDQxNjU4NTRaMBQx +EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBALgagHqEqWr4WH+MFBQE+BWZri5UUn/QPORN2pUB1lWMzeDCM5YMc/D1dhUG +7zg5I9QO5Ut1YcoVO25OAseddgVaIFXPNyEG2nUTz53xx3pyqp3WtQkYCRAQzI8K +IFIzBSD+nJNl+8gBld7Fe+4d8bFCwfXspQBJ2RY8SQ6tjRFVKHN7haLsD+WV3AFg +siWkCxeXxVLNI69cuLwV7bEsv6U1N1yNROvRpu4yJcaNnu36kJFbORPhNfy6qJGX +i0A30dYdMoLhtCN3Qf/XwGyS84Rs2XXduNlBdUgbpluY2r2x3Gz32hIwsHHcPzX6 +O9nwVPQ8k29lfC8yPmAWA9vPiBUCAwEAAaNQME4wHQYDVR0OBBYEFJZESCN01tY3 +MgXxmqiUBNPxsgiKMB8GA1UdIwQYMBaAFJZESCN01tY3MgXxmqiUBNPxsgiKMAwG +A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAHylCJYED9PqLB9FE4A0CRfy +BdxIqOK+UExxxkU1DeN7kM4U2+E0G85nqBLOL34BDj8LDKJH9WC7L9jMV8T3Upbg ++RrTGiIcyjsL18L2KWeOia1R6VVAQcZrqoWv+QXyVvIi8IpTOE074C6+Vzx6XYMe +CpW4jcdfmn39oVeMXxz9+8wD7CWeCT+SMj8tt+OB1XjQwdEG03vb6ArtnuJT77VI +3I090OtKksBE5hy1H9N2E3wxhFTxC+DI5sc7Bj87v3blL4Z3DvRUEHwQHcDccQ0D +ERUEcSyn1YGSlDVbVf3CzH4WXxddUBmaSHf4JTuAMy0C0A6IWuMP+rVVvVMIXNM= +-----END CERTIFICATE----- diff --git a/examples/websockets/sslechoserver/localhost.key b/examples/websockets/sslechoserver/localhost.key new file mode 100644 index 0000000..dcc262c --- /dev/null +++ b/examples/websockets/sslechoserver/localhost.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAuBqAeoSpavhYf4wUFAT4FZmuLlRSf9A85E3alQHWVYzN4MIz +lgxz8PV2FQbvODkj1A7lS3VhyhU7bk4Cx512BVogVc83IQbadRPPnfHHenKqnda1 +CRgJEBDMjwogUjMFIP6ck2X7yAGV3sV77h3xsULB9eylAEnZFjxJDq2NEVUoc3uF +ouwP5ZXcAWCyJaQLF5fFUs0jr1y4vBXtsSy/pTU3XI1E69Gm7jIlxo2e7fqQkVs5 +E+E1/LqokZeLQDfR1h0yguG0I3dB/9fAbJLzhGzZdd242UF1SBumW5javbHcbPfa +EjCwcdw/Nfo72fBU9DyTb2V8LzI+YBYD28+IFQIDAQABAoIBADADmWZamuSvCEWb +ftEJyrm3btneW/XBlV/lfhBGfmOpaTgo7dNARCyfl8A8Ir+DB3kSuXJIlsxhZTKL +XeY5hsI2kyBN/o66ftbx57/FgsTIiv7HNEe+4P4zo2Xaujs12yyTuromatfqse97 +iaq8XIBibLt6kD3XBKrr5hHR6WyPW9F7sM91e/1OGTXBt9URvnnHqcimJ8Wvto5p +SMFlud8JXMmARhWaIfqQIcNYmNEHea0LfOZVerTOPE7IZd5coM91mr71lzoxs9Ik +bRMbqgyAMXEJV3ynEH2LjjJZo1bVb8Va45QqqOQ4FtL46n1Z+EVUXyhCHGepwyOv +uAru0wECgYEA3CnM252417At+N9Zq4KtBJ8tIEXKqvK1Bl5aZSqaJClywjCttR2l +lRAlIRAmqHCQuxLDDfLfiJh6xgZsjr4MhksuyKc7DKssfWW+XCd+5GaaMoMvJXhs +caJdhNSHzpnLPi++tJBYHwHa354D5PJ3eItzLkmuVyhmc3plNbkBGLECgYEA1hIX +mA9KadVG9VkaDci+xo1p0ACb5ccHAvmA8+fzb0H09yKw5bCSpNGeHj4Qv5ZFqsUm +96NWYEB38ezupWqWMFd9zr1kD2s3r6Kpq1TS6xuDPapyiXaKpcH1ys7IWZkinCkN +oxhPHcQbFu9/CN2zIVFKvF5P98Rh0+wO7fgL/qUCgYAyleAd7cVUuYQ2lIrz31iF +oVUq/x4r3Qw+5Fr8t6cm++cEO08OODudXlJJoH5hYUu/z/XFNLKrne1Hpp42xe/2 +wzuCmvn0VTgpr8DnR6PeMrznMkEY6Oxxx46ZQIPbsnaCWVIGBsxYg4KYsqPObO+K +YPAcCI3oNL4ldk/e4h0gYQKBgHm+4FxClfeKrEhs3DxqptUVJ9B8CC2t+3bdn1EX +4YKs6DyFJkX7HetOq7ZXZf8P3583cOn1ovIquAyGy1KYQ8JKf+pMG9QJDip9QDGI +lEvR4dn4ThuPp1qN7NPitl7+kIhvcKoI8TXkieOJYZ4ROAcCzJZErQYkUd7MqdD8 ++RVhAoGAcWC9HRDhxjs4shaBlYi8Lfp3dV4f/4UgYosdFAZ26atwHz+sCLHwwg1j +6t5Zxy9oEB89S5v5hkgO8//JmopvISSokdvocASMdKE+OmS3JfBmhQK9qVBW/vv/ +ut2bhPjEzIJyNFKX3xnGI8PREcR2eY+WLhIZ5KiR61tGpktJ4bg= +-----END RSA PRIVATE KEY----- diff --git a/examples/websockets/sslechoserver/main.cpp b/examples/websockets/sslechoserver/main.cpp new file mode 100644 index 0000000..5b4ff4e --- /dev/null +++ b/examples/websockets/sslechoserver/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 <QtCore/QCoreApplication> +#include "sslechoserver.h" + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + SslEchoServer server(1234); + + Q_UNUSED(server); + + return a.exec(); +} diff --git a/examples/websockets/sslechoserver/sslechoclient.html b/examples/websockets/sslechoserver/sslechoclient.html new file mode 100644 index 0000000..b9faa44 --- /dev/null +++ b/examples/websockets/sslechoserver/sslechoclient.html @@ -0,0 +1,104 @@ +<!DOCTYPE html> +<html> + <head> + <title>Websocket Echo Client</title> + <meta charset="utf-8"> + </head> + <body> + <h1>Websocket Echo Client</h1> + <p> + <button onClick="initWebsocket();">Connect</button> + <button onClick="stopWebsocket();">Disconnect</button> + <button onClick="checkSocket();">State</button> + </p> + <p> + <textarea id="debugTextArea" style="width:400px;height:200px;"></textarea> + </p> + <p> + <input type="text" id="inputText" onkeydown="if(event.keyCode==13)sendMessage();"/> + <button onClick="sendMessage();">Send</button> + </p> + + <script type="text/javascript"> + var debugTextArea = document.getElementById("debugTextArea"); + var wsUri = "wss://localhost:1234"; + var websocket = null; + + function debug(message) { + debugTextArea.value += message + "\n"; + debugTextArea.scrollTop = debugTextArea.scrollHeight; + } + + function sendMessage() { + var msg = document.getElementById("inputText").value; + if ( websocket != null ) + { + document.getElementById("inputText").value = ""; + websocket.send( msg ); + console.log( "string sent :", '"'+msg+'"' ); + } + } + + function initWebsocket() { + try { + if (typeof MozWebSocket == 'function') + WebSocket = MozWebSocket; + if ( websocket && websocket.readyState == 1 ) + websocket.close(); + websocket = new WebSocket( wsUri ); + websocket.onopen = function (evt) { + debug("CONNECTED"); + }; + websocket.onclose = function (evt) { + debug("DISCONNECTED"); + }; + websocket.onmessage = function (evt) { + console.log( "Message received :", evt.data ); + debug( evt.data ); + }; + websocket.onerror = function (evt) { + debug('ERROR: ' + evt.data); + }; + } catch (exception) { + debug('ERROR: ' + exception); + } + } + + function stopWebsocket() { + if (websocket) + websocket.close(); + } + + function checkSocket() { + if (websocket != null) { + var stateStr; + switch (websocket.readyState) { + case 0: { + stateStr = "CONNECTING"; + break; + } + case 1: { + stateStr = "OPEN"; + break; + } + case 2: { + stateStr = "CLOSING"; + break; + } + case 3: { + stateStr = "CLOSED"; + break; + } + default: { + stateStr = "UNKNOW"; + break; + } + } + debug("Websocket state = " + websocket.readyState + " ( " + stateStr + " )"); + } else { + debug("Websocket is null"); + } + } + </script> + </body> +</html> diff --git a/examples/websockets/sslechoserver/sslechoserver.cpp b/examples/websockets/sslechoserver/sslechoserver.cpp new file mode 100644 index 0000000..d93e5c6 --- /dev/null +++ b/examples/websockets/sslechoserver/sslechoserver.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 "sslechoserver.h" +#include "QtWebSockets/QWebSocketServer" +#include "QtWebSockets/QWebSocket" +#include <QtCore/QDebug> +#include <QtCore/QFile> +#include <QtNetwork/QSslCertificate> +#include <QtNetwork/QSslKey> + +QT_USE_NAMESPACE + +//! [constructor] +SslEchoServer::SslEchoServer(quint16 port, QObject *parent) : + QObject(parent), + m_pWebSocketServer(Q_NULLPTR), + m_clients() +{ + m_pWebSocketServer = new QWebSocketServer(QStringLiteral("SSL Echo Server"), + QWebSocketServer::SecureMode, + this); + QSslConfiguration sslConfiguration; + QFile certFile(QStringLiteral("./localhost.cert")); + QFile keyFile(QStringLiteral("./localhost.key")); + certFile.open(QIODevice::ReadOnly); + keyFile.open(QIODevice::ReadOnly); + QSslCertificate certificate(&certFile, QSsl::Pem); + QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem); + certFile.close(); + keyFile.close(); + sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone); + sslConfiguration.setLocalCertificate(certificate); + sslConfiguration.setPrivateKey(sslKey); + sslConfiguration.setProtocol(QSsl::TlsV1SslV3); + m_pWebSocketServer->setSslConfiguration(sslConfiguration); + + if (m_pWebSocketServer->listen(QHostAddress::Any, port)) + { + qDebug() << "SSL Echo Server listening on port" << port; + connect(m_pWebSocketServer, &QWebSocketServer::newConnection, + this, &SslEchoServer::onNewConnection); + connect(m_pWebSocketServer, &QWebSocketServer::sslErrors, + this, &SslEchoServer::onSslErrors); + } +} +//! [constructor] + +SslEchoServer::~SslEchoServer() +{ + m_pWebSocketServer->close(); + qDeleteAll(m_clients.begin(), m_clients.end()); +} + +//! [onNewConnection] +void SslEchoServer::onNewConnection() +{ + QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); + + qDebug() << "Client connected:" << pSocket->peerName() << pSocket->origin(); + + connect(pSocket, &QWebSocket::textMessageReceived, this, &SslEchoServer::processTextMessage); + connect(pSocket, &QWebSocket::binaryMessageReceived, + this, &SslEchoServer::processBinaryMessage); + connect(pSocket, &QWebSocket::disconnected, this, &SslEchoServer::socketDisconnected); + //connect(pSocket, &QWebSocket::pong, this, &SslEchoServer::processPong); + + m_clients << pSocket; +} +//! [onNewConnection] + +//! [processTextMessage] +void SslEchoServer::processTextMessage(QString message) +{ + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) + { + pClient->sendTextMessage(message); + } +} +//! [processTextMessage] + +//! [processBinaryMessage] +void SslEchoServer::processBinaryMessage(QByteArray message) +{ + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) + { + pClient->sendBinaryMessage(message); + } +} +//! [processBinaryMessage] + +//! [socketDisconnected] +void SslEchoServer::socketDisconnected() +{ + qDebug() << "Client disconnected"; + QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (pClient) + { + m_clients.removeAll(pClient); + pClient->deleteLater(); + } +} + +void SslEchoServer::onSslErrors(const QList<QSslError> &) +{ + qDebug() << "Ssl errors occurred"; +} +//! [socketDisconnected] diff --git a/examples/websockets/sslechoserver/sslechoserver.h b/examples/websockets/sslechoserver/sslechoserver.h new file mode 100644 index 0000000..f4e9a9b --- /dev/null +++ b/examples/websockets/sslechoserver/sslechoserver.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebSockets 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 SSLECHOSERVER_H +#define SSLECHOSERVER_H + +#include <QtCore/QObject> +#include <QtCore/QList> +#include <QtCore/QByteArray> +#include <QtNetwork/QSslError> + +QT_FORWARD_DECLARE_CLASS(QWebSocketServer) +QT_FORWARD_DECLARE_CLASS(QWebSocket) + +class SslEchoServer : public QObject +{ + Q_OBJECT +public: + explicit SslEchoServer(quint16 port, QObject *parent = Q_NULLPTR); + virtual ~SslEchoServer(); + +private Q_SLOTS: + void onNewConnection(); + void processTextMessage(QString message); + void processBinaryMessage(QByteArray message); + void socketDisconnected(); + void onSslErrors(const QList<QSslError> &errors); + +private: + QWebSocketServer *m_pWebSocketServer; + QList<QWebSocket *> m_clients; +}; + +#endif //SSLECHOSERVER_H diff --git a/examples/websockets/sslechoserver/sslechoserver.pro b/examples/websockets/sslechoserver/sslechoserver.pro new file mode 100644 index 0000000..2663872 --- /dev/null +++ b/examples/websockets/sslechoserver/sslechoserver.pro @@ -0,0 +1,17 @@ +QT += core websockets +QT -= gui + +TARGET = sslechoserver +CONFIG += console +CONFIG -= app_bundle + +TEMPLATE = app + +SOURCES += \ + main.cpp \ + sslechoserver.cpp + +HEADERS += \ + sslechoserver.h + +OTHER_FILES += sslechoclient.html diff --git a/examples/websockets/websockets.pro b/examples/websockets/websockets.pro new file mode 100644 index 0000000..76a0e9f --- /dev/null +++ b/examples/websockets/websockets.pro @@ -0,0 +1,12 @@ +TEMPLATE = subdirs + +SUBDIRS = echoclient \ + echoserver \ + simplechat +qtHaveModule(quick): SUBDIRS += qmlwebsocketclient + +contains(QT_CONFIG, openssl) | contains(QT_CONFIG, openssl-linked) { +SUBDIRS += \ + sslechoserver \ + sslechoclient +} |