**
****************************************************************************/
#include "echoclient.h"
-#include <QDebug>
+#include <QtCore/QDebug>
QT_USE_NAMESPACE
#ifndef ECHOCLIENT_H
#define ECHOCLIENT_H
-#include <QObject>
+#include <QtCore/QObject>
#include <QtWebSockets/QWebSocket>
-QT_FORWARD_DECLARE_CLASS(QWebSocket);
+QT_FORWARD_DECLARE_CLASS(QWebSocket)
class EchoClient : public QObject
{
** $QT_END_LICENSE$
**
****************************************************************************/
-#include <QCoreApplication>
+#include <QtCore/QCoreApplication>
#include "echoclient.h"
int main(int argc, char *argv[])
**
****************************************************************************/
#include "echoserver.h"
-#include "qwebsocketserver.h"
-#include "qwebsocket.h"
-#include <QDebug>
+#include "QtWebSockets/qwebsocketserver.h"
+#include "QtWebSockets/qwebsocket.h"
+#include <QtCore/QDebug>
QT_USE_NAMESPACE
m_pWebSocketServer(Q_NULLPTR),
m_clients()
{
- m_pWebSocketServer = new QWebSocketServer("Echo Server", this);
+ m_pWebSocketServer = new QWebSocketServer("Echo Server", QWebSocketServer::NON_SECURE_MODE, this);
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
qDebug() << "Echoserver listening on port" << port;
#ifndef ECHOSERVER_H
#define ECHOSERVER_H
-#include <QObject>
-#include <QList>
-#include <QByteArray>
+#include <QtCore/QObject>
+#include <QtCore/QList>
+#include <QtCore/QByteArray>
-QT_FORWARD_DECLARE_CLASS(QWebSocketServer);
-QT_FORWARD_DECLARE_CLASS(QWebSocket);
+QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
+QT_FORWARD_DECLARE_CLASS(QWebSocket)
class EchoServer : public QObject
{
** $QT_END_LICENSE$
**
****************************************************************************/
-#include <QCoreApplication>
+#include <QtCore/QCoreApplication>
#include "echoserver.h"
int main(int argc, char *argv[])
SUBDIRS = echoclient \
echoserver \
+ sslechoserver \
+ sslechoclient \
simplechat
qtHaveModule(quick): SUBDIRS += qmlwebsocketclient
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+#include <QtCore/QCoreApplication>
+#include "sslechoclient.h"
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication a(argc, argv);
+ //SslEchoClient client(QUrl("wss://localhost:1234"));
+ SslEchoClient client(QUrl("wss://echo.websocket.org"));
+
+ Q_UNUSED(client);
+
+ return a.exec();
+}
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+#include "sslechoclient.h"
+#include <QtCore/QDebug>
+
+QT_USE_NAMESPACE
+
+//! [constructor]
+SslEchoClient::SslEchoClient(const QUrl &url, QObject *parent) :
+ QObject(parent),
+ m_webSocket()
+{
+ connect(&m_webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
+ m_webSocket.open(QUrl(url));
+}
+//! [constructor]
+
+//! [onConnected]
+void SslEchoClient::onConnected()
+{
+ qDebug() << "Websocket connected";
+ connect(&m_webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)));
+ m_webSocket.write("Hello, world!");
+}
+//! [onConnected]
+
+//! [onTextMessageReceived]
+void SslEchoClient::onTextMessageReceived(QString message)
+{
+ qDebug() << "Message received:" << message;
+}
+//! [onTextMessageReceived]
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+#ifndef SSLECHOCLIENT_H
+#define SSLECHOCLIENT_H
+
+#include <QtCore/QObject>
+#include <QtWebSockets/QWebSocket>
+
+QT_FORWARD_DECLARE_CLASS(QWebSocket)
+
+class SslEchoClient : public QObject
+{
+ Q_OBJECT
+public:
+ explicit SslEchoClient(const QUrl &url, QObject *parent = Q_NULLPTR);
+
+Q_SIGNALS:
+
+public Q_SLOTS:
+
+private Q_SLOTS:
+ void onConnected();
+ void onTextMessageReceived(QString message);
+
+private:
+ QWebSocket m_webSocket;
+};
+
+#endif // SSLECHOCLIENT_H
--- /dev/null
+QT += core websockets
+QT -= gui
+
+TARGET = sslechoclient
+CONFIG += console
+CONFIG -= app_bundle
+
+TEMPLATE = app
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pNGeHj4Qv5ZFqsUm
+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-----
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+#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();
+}
--- /dev/null
+<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");
+ 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>
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+#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("SSL Echo Server", QWebSocketServer::SECURE_MODE, 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.setProtocol(QSsl::AnyProtocol);
+ 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, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
+ }
+}
+//! [constructor]
+
+//! [onNewConnection]
+void SslEchoServer::onNewConnection()
+{
+ QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
+
+ qDebug() << "Client connected:" << pSocket->peerName() << pSocket->origin();
+
+ connect(pSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(processMessage(QString)));
+ connect(pSocket, SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(processBinaryMessage(QByteArray)));
+ connect(pSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
+ //connect(pSocket, SIGNAL(pong(quint64)), this, SLOT(processPong(quint64)));
+
+ m_clients << pSocket;
+}
+//! [onNewConnection]
+
+//! [processMessage]
+void SslEchoServer::processMessage(QString message)
+{
+ QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
+ if (pClient)
+ {
+ pClient->write(message);
+ }
+}
+//! [processMessage]
+
+//! [processBinaryMessage]
+void SslEchoServer::processBinaryMessage(QByteArray message)
+{
+ QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
+ if (pClient)
+ {
+ pClient->write(message);
+ }
+}
+//! [processBinaryMessage]
+
+//! [socketDisconnected]
+void SslEchoServer::socketDisconnected()
+{
+ qDebug() << "Client disconnected";
+ QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
+ if (pClient)
+ {
+ m_clients.removeAll(pClient);
+ pClient->deleteLater();
+ }
+}
+//! [socketDisconnected]
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+#ifndef SSLECHOSERVER_H
+#define SSLECHOSERVER_H
+
+#include <QtCore/QObject>
+#include <QtCore/QList>
+#include <QtCore/QByteArray>
+
+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);
+
+Q_SIGNALS:
+
+private Q_SLOTS:
+ void onNewConnection();
+ void processMessage(QString message);
+ void processBinaryMessage(QByteArray message);
+ void socketDisconnected();
+
+private:
+ QWebSocketServer *m_pWebSocketServer;
+ QList<QWebSocket *> m_clients;
+};
+
+#endif //SSLECHOSERVER_H
--- /dev/null
+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
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+
+#include "qsslserver_p.h"
+
+#include <QtNetwork/QSslSocket>
+#include <QtNetwork/QSslCipher>
+
+QSslServer::QSslServer(QObject *parent) :
+ QTcpServer(parent),
+ m_sslConfiguration(QSslConfiguration::defaultConfiguration())
+{
+}
+
+QSslServer::~QSslServer()
+{
+}
+
+void QSslServer::setSslConfiguration(const QSslConfiguration &sslConfiguration)
+{
+ m_sslConfiguration = sslConfiguration;
+}
+
+QSslConfiguration QSslServer::sslConfiguration() const
+{
+ return m_sslConfiguration;
+}
+
+void QSslServer::incomingConnection(qintptr socket)
+{
+ QSslSocket *pSslSocket = new QSslSocket();
+
+ pSslSocket->setSslConfiguration(m_sslConfiguration);
+
+ if (pSslSocket->setSocketDescriptor(socket))
+ {
+ connect(pSslSocket, SIGNAL(peerVerifyError(QSslError)), this, SIGNAL(peerVerifyError(QSslError)));
+ connect(pSslSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SIGNAL(sslErrors(QList<QSslError>)));
+ connect(pSslSocket, SIGNAL(encrypted()), this, SIGNAL(newEncryptedConnection()));
+
+ addPendingConnection(pSslSocket);
+
+ pSslSocket->startServerEncryption();
+ }
+ else
+ {
+ delete pSslSocket;
+ }
+}
--- /dev/null
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+
+#ifndef QSSLSERVER_P_H
+#define QSSLSERVER_P_H
+
+#include <QtNetwork/QTcpServer>
+#include <QtNetwork/QSslError>
+#include <QtNetwork/QSslConfiguration>
+#include <QtCore/QList>
+
+QT_BEGIN_NAMESPACE
+
+class QSslServer : public QTcpServer
+{
+ Q_OBJECT
+ Q_DISABLE_COPY(QSslServer)
+
+public:
+ QSslServer(QObject *parent = Q_NULLPTR);
+ virtual ~QSslServer();
+
+ void setSslConfiguration(const QSslConfiguration &sslConfiguration);
+ QSslConfiguration sslConfiguration() const;
+
+Q_SIGNALS:
+ void sslErrors(const QList<QSslError> &errors);
+ void peerVerifyError(const QSslError &error);
+ void newEncryptedConnection();
+
+protected:
+ virtual void incomingConnection(qintptr socket);
+
+private:
+ QSslConfiguration m_sslConfiguration;
+};
+
+QT_END_NAMESPACE
+
+#endif // QSSLSERVER_P_H
//catch signals
connect(pTcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(processStateChanged(QAbstractSocket::SocketState)));
- connect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(processData()));
+ //!!!important to use a QueuedConnection here; with QTcpSocket there is no problem, but with QSslSocket the processing hangs
+ connect(pTcpSocket, SIGNAL(readyRead()), this, SLOT(processData()), Qt::QueuedConnection);
connect(&m_dataProcessor, SIGNAL(textFrameReceived(QString,bool)), q, SIGNAL(textFrameReceived(QString,bool)));
connect(&m_dataProcessor, SIGNAL(binaryFrameReceived(QByteArray,bool)), q, SIGNAL(binaryFrameReceived(QByteArray,bool)));
{
if (pIoDevice->bytesAvailable() >= 4)
{
- //TODO: big endian conversion?
bytesRead = pIoDevice->read(reinterpret_cast<char *>(&frame.m_mask), sizeof(frame.m_mask));
if (bytesRead == -1)
{
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QNetworkProxy>
+#ifndef QT_NO_SSL
+#include <QtNetwork/QSslConfiguration>
+#endif
+
QT_BEGIN_NAMESPACE
/*!
Constructs a new WebSocketServer with the given \a serverName.
The \a serverName will be used in the http handshake phase to identify the server.
+
\a parent is passed to the QObject constructor.
*/
-QWebSocketServer::QWebSocketServer(const QString &serverName, QObject *parent) :
+QWebSocketServer::QWebSocketServer(const QString &serverName, SecureMode secureMode, QObject *parent) :
QObject(parent),
- d_ptr(new QWebSocketServerPrivate(serverName, this, this))
+ d_ptr(new QWebSocketServerPrivate(serverName,
+ (secureMode == SECURE_MODE) ? QWebSocketServerPrivate::SECURE_MODE : QWebSocketServerPrivate::NON_SECURE_MODE,
+ this,
+ this))
{
}
d->setProxy(networkProxy);
}
#endif
+
+#ifndef QT_NO_SSL
+/*!
+ Sets the SSL configuration for the websocket server to \a sslConfiguration.
+ This method has no effect if QWebSocketServer runs in non-secure mode (QWebSocketServer::NON_SECURE_MODE).
+
+ \sa sslConfiguration(), SecureMode
+ */
+void QWebSocketServer::setSslConfiguration(const QSslConfiguration &sslConfiguration)
+{
+ Q_D(QWebSocketServer);
+ d->setSslConfiguration(sslConfiguration);
+}
+
+/*!
+ Returns the SSL configuration used by the websocket server.
+ If the server is not running in secure mode (QWebSocketServer::SECURE_MODE),
+ this method returns QSslConfiguration::defaultConfiguration().
+
+ \sa sslConfiguration(), SecureMode, QSslConfiguration::defaultConfiguration()
+ */
+QSslConfiguration QWebSocketServer::sslConfiguration() const
+{
+ Q_D(const QWebSocketServer);
+ return d->sslConfiguration();
+}
+#endif
+
/*!
Resumes accepting new connections.
\sa pauseAccepting()
}
/*!
+ Returns the mode the server is running in.
+
+ \sa QWebSocketServer(), SecureMode
+ */
+QWebSocketServer::SecureMode QWebSocketServer::secureMode() const
+{
+ Q_D(const QWebSocketServer);
+ return (d->secureMode() == QWebSocketServerPrivate::SECURE_MODE) ?
+ QWebSocketServer::SECURE_MODE : QWebSocketServer::NON_SECURE_MODE;
+}
+
+/*!
Returns an error code for the last error that occurred.
\sa errorString()
*/
#ifndef QWEBSOCKETSERVER_H
#define QWEBSOCKETSERVER_H
+#include "QtWebSockets/qwebsockets_global.h"
+#include "QtWebSockets/qwebsocketprotocol.h"
+
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtNetwork/QHostAddress>
-#include "QtWebSockets/qwebsockets_global.h"
-#include "QtWebSockets/qwebsocketprotocol.h"
+#ifndef QT_NO_SSL
+#include <QtNetwork/QSslConfiguration>
+#include <QtNetwork/QSslError>
+#endif
QT_BEGIN_NAMESPACE
Q_DISABLE_COPY(QWebSocketServer)
Q_DECLARE_PRIVATE(QWebSocketServer)
+ Q_ENUMS(SecureMode)
+
public:
- explicit QWebSocketServer(const QString &serverName, QObject *parent = Q_NULLPTR);
+ enum SecureMode {
+#ifndef QT_NO_SSL
+ SECURE_MODE,
+#endif
+ NON_SECURE_MODE
+ };
+
+ explicit QWebSocketServer(const QString &serverName, SecureMode secureMode, QObject *parent = Q_NULLPTR);
virtual ~QWebSocketServer();
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
quint16 serverPort() const;
QHostAddress serverAddress() const;
+ SecureMode secureMode() const;
+
bool setSocketDescriptor(int socketDescriptor);
int socketDescriptor() const;
void setProxy(const QNetworkProxy &networkProxy);
QNetworkProxy proxy() const;
#endif
+#ifndef QT_NO_SSL
+ void setSslConfiguration(const QSslConfiguration &sslConfiguration);
+ QSslConfiguration sslConfiguration() const;
+#endif
QList<QWebSocketProtocol::Version> supportedVersions() const;
QList<QString> supportedProtocols() const;
Q_SIGNALS:
void acceptError(QAbstractSocket::SocketError socketError);
+ //TODO: should use a delegate iso of a synchronous signal
void originAuthenticationRequired(QWebSocketCorsAuthenticator *pAuthenticator);
void newConnection();
+#ifndef QT_NO_SSL
+ void peerVerifyError(const QSslError &error);
+ void sslErrors(const QList<QSslError> &errors);
+#endif
private:
QWebSocketServerPrivate * const d_ptr;
#include "qwebsocketserver.h"
#include "qwebsocketserver_p.h"
+#ifndef QT_NO_SSL
+#include "qsslserver_p.h"
+#endif
#include "qwebsocketprotocol.h"
#include "qwebsockethandshakerequest_p.h"
#include "qwebsockethandshakeresponse_p.h"
/*!
\internal
*/
-QWebSocketServerPrivate::QWebSocketServerPrivate(const QString &serverName, QWebSocketServer * const pWebSocketServer, QObject *parent) :
+QWebSocketServerPrivate::QWebSocketServerPrivate(const QString &serverName, QWebSocketServerPrivate::SecureMode secureMode,
+ QWebSocketServer * const pWebSocketServer, QObject *parent) :
QObject(parent),
q_ptr(pWebSocketServer),
m_pTcpServer(Q_NULLPTR),
m_serverName(serverName),
+ m_secureMode(secureMode),
m_pendingConnections()
{
Q_ASSERT(pWebSocketServer);
- m_pTcpServer = new QTcpServer(this);
+ if (m_secureMode == NON_SECURE_MODE)
+ {
+ m_pTcpServer = new QTcpServer(this);
+ connect(m_pTcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
+ }
+ else
+ {
+#ifndef QT_NO_SSL
+ QSslServer *pSslServer = new QSslServer(this);
+ m_pTcpServer = pSslServer;
+ connect(pSslServer, SIGNAL(newEncryptedConnection()), this, SLOT(onNewConnection()));
+ connect(pSslServer, SIGNAL(peerVerifyError(QSslError)), q_ptr, SIGNAL(peerVerifyError(QSslError)));
+ connect(pSslServer, SIGNAL(sslErrors(QList<QSslError>)), q_ptr, SIGNAL(sslErrors(QList<QSslError>)));
+#endif
+ }
connect(m_pTcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), q_ptr, SIGNAL(acceptError(QAbstractSocket::SocketError)));
- connect(m_pTcpServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
/*!
}
/*!
+ \internal
+ */
+QWebSocketServerPrivate::SecureMode QWebSocketServerPrivate::secureMode() const
+{
+ return m_secureMode;
+}
+
+void QWebSocketServerPrivate::setSslConfiguration(const QSslConfiguration &sslConfiguration)
+{
+ if (m_secureMode == SECURE_MODE)
+ {
+ qobject_cast<QSslServer *>(m_pTcpServer)->setSslConfiguration(sslConfiguration);
+ }
+}
+
+QSslConfiguration QWebSocketServerPrivate::sslConfiguration() const
+{
+ if (m_secureMode == SECURE_MODE)
+ {
+ return qobject_cast<QSslServer *>(m_pTcpServer)->sslConfiguration();
+ }
+ else
+ {
+ return QSslConfiguration::defaultConfiguration();
+ }
+}
+
+/*!
\internal
*/
void QWebSocketServerPrivate::onNewConnection()
#include <QtNetwork/QHostAddress>
#include "qwebsocket.h"
+#ifndef QT_NO_SSL
+#include <QtNetwork/QSslConfiguration>
+#include <QtNetwork/QSslError>
+#endif
+
QT_BEGIN_NAMESPACE
class QTcpServer;
Q_DECLARE_PUBLIC(QWebSocketServer)
public:
- explicit QWebSocketServerPrivate(const QString &serverName, QWebSocketServer * const pWebSocketServer, QObject *parent = Q_NULLPTR);
+ enum SecureMode
+ {
+ SECURE_MODE = true,
+ NON_SECURE_MODE
+ };
+
+ explicit QWebSocketServerPrivate(const QString &serverName, SecureMode secureMode, QWebSocketServer * const pWebSocketServer, QObject *parent = Q_NULLPTR);
virtual ~QWebSocketServerPrivate();
void close();
void setServerName(const QString &serverName);
QString serverName() const;
-Q_SIGNALS:
- void newConnection();
+ SecureMode secureMode() const;
+
+#ifndef QT_NO_SSL
+ void setSslConfiguration(const QSslConfiguration &sslConfiguration);
+ QSslConfiguration sslConfiguration() const;
+#endif
private Q_SLOTS:
void onNewConnection();
QTcpServer *m_pTcpServer;
QString m_serverName;
+ SecureMode m_secureMode;
QQueue<QWebSocket *> m_pendingConnections;
void addPendingConnection(QWebSocket *pWebSocket);
$$PWD/qwebsocketcorsauthenticator.cpp \
$$PWD/qwebsocketframe_p.cpp
+contains(QT_CONFIG, openssl) | contains(QT_CONFIG, openssl-linked) {
+ SOURCES += $$PWD/qsslserver_p.cpp
+ PRIVATE_HEADERS += $$PWD/qsslserver_p.h
+}
+
HEADERS += $$PUBLIC_HEADERS $$PRIVATE_HEADERS
load(qt_module)