1 /****************************************************************************
3 ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtWebSocket module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** In addition, as a special exception, Digia gives you certain additional
27 ** rights. These rights are described in the Digia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32 ****************************************************************************/
34 #include "qqmlwebsocketserver.h"
35 #include "qqmlwebsocket.h"
40 \qmltype WebSocketServer
41 \instantiates QQmlWebSocketServer
43 \inqmlmodule Qt.WebSockets
44 \ingroup websockets-qml
45 \brief QML interface to QWebSocketServer.
49 \qmlproperty QUrl WebSocketServer::url
50 Server url that client WebSockets can connect to. The url uses the \e ws:// scheme and includes the
51 port the server listens to and the host address of the server.
55 \qmlproperty QString WebSocketServer::host
56 The host address of the server. By default, localhost is used.
60 \qmlproperty int WebSocketServer::port
61 The port this server is listening on. By default, a port is chosen automatically.
65 \qmlproperty QString WebSocketServer::name
66 The name of this server used during the http handshake phase.
70 \qmlproperty QString WebSocketServer::errorString
71 The stringified error message in case an error occurred.
75 \qmlproperty bool WebSocketServer::listen
76 Set to true when the server should listen to client connections or false otherwise.
77 When set to true, the server will listen on the specified url defined by host and port
78 and, when accept is true, accepts incoming client connections. Otherwise the server is closed.
79 By default, the server is not listening.
83 \qmlproperty bool WebSocketServer::accept
84 Set to true to accept incoming client connections when the server is listening. When set to false,
85 incoming connections are rejected. By default, connections are accepted.
89 \qmlsignal WebSocketServer::clientConnected(WebSocket webSocket)
90 This signal is emitted when a client connects to this server.
93 QQmlWebSocketServer::QQmlWebSocketServer(QObject *parent)
95 , m_host(QHostAddress(QHostAddress::LocalHost).toString())
99 , m_componentCompleted(true)
103 QQmlWebSocketServer::~QQmlWebSocketServer()
108 void QQmlWebSocketServer::classBegin()
110 m_componentCompleted = false;
113 void QQmlWebSocketServer::componentComplete()
116 m_componentCompleted = true;
119 QUrl QQmlWebSocketServer::url() const
128 QString QQmlWebSocketServer::host() const
133 void QQmlWebSocketServer::setHost(const QString &host)
135 if (host == m_host) {
140 emit hostChanged(host);
141 emit urlChanged(url());
146 quint16 QQmlWebSocketServer::port() const
151 void QQmlWebSocketServer::setPort(quint16 port)
153 if (port == m_port) {
158 emit portChanged(port);
159 emit urlChanged(url());
161 if (m_componentCompleted && m_server->isListening()) {
166 QString QQmlWebSocketServer::errorString() const
168 return m_server ? m_server->errorString() : tr("QQmlWebSocketServer is not ready.");
171 QString QQmlWebSocketServer::name() const
176 void QQmlWebSocketServer::setName(const QString &name)
178 if (name == m_name) {
183 emit nameChanged(name);
185 if (m_componentCompleted) {
190 bool QQmlWebSocketServer::listen() const
195 void QQmlWebSocketServer::setListen(bool listen)
197 if (listen == m_listen) {
202 emit listenChanged(listen);
207 bool QQmlWebSocketServer::accept() const
212 void QQmlWebSocketServer::setAccept(bool accept)
214 if (accept == m_accept) {
219 emit acceptChanged(accept);
221 if (m_componentCompleted) {
223 m_server->pauseAccepting();
225 m_server->resumeAccepting();
230 void QQmlWebSocketServer::init()
232 // TODO: add support for wss, requires ssl configuration to be set from QML - realistic?
233 m_server.reset(new QWebSocketServer(m_name, QWebSocketServer::NonSecureMode));
235 connect(m_server.data(), &QWebSocketServer::newConnection,
236 this, &QQmlWebSocketServer::newConnection);
237 connect(m_server.data(), &QWebSocketServer::serverError,
238 this, &QQmlWebSocketServer::serverError);
239 connect(m_server.data(), &QWebSocketServer::closed,
240 this, &QQmlWebSocketServer::closed);
245 void QQmlWebSocketServer::updateListening()
251 if (m_server->isListening()) {
255 if (!m_listen || !m_server->listen(QHostAddress(m_host), m_port)) {
258 setPort(m_server->serverPort());
259 setHost(m_server->serverAddress().toString());
262 void QQmlWebSocketServer::newConnection()
264 emit clientConnected(new QQmlWebSocket(m_server->nextPendingConnection(), this));
267 void QQmlWebSocketServer::serverError()
269 emit errorStringChanged(errorString());
272 void QQmlWebSocketServer::closed()