1 /****************************************************************************
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtWebSockets 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 ****************************************************************************/
36 \instantiates QQmlWebSocket
38 \inqmlmodule Qt.WebSockets
39 \ingroup websockets-qml
40 \brief QML interface to QWebSocket.
42 WebSockets is a web technology providing full-duplex communications channels over a
43 single TCP connection.
44 The WebSocket protocol was standardized by the IETF as
45 \l {http://tools.ietf.org/html/rfc6455} {RFC 6455} in 2011.
49 \qmlproperty QUrl WebSocket::url
50 Server url to connect to. The url must have one of 2 schemes: \e ws:// or \e wss://.
51 When not supplied, then \e ws:// is used.
55 \qmlproperty Status WebSocket::status
56 Status of the WebSocket.
58 The status can have the following values:
60 \li WebSockets.Connecting
62 \li WebSockets.Closing
69 \qmlproperty QString WebSocket::errorString
70 Contains a description of the last error that occurred. When no error occurrred,
75 \qmlproperty bool WebSocket::active
76 When set to true, a connection is made to the server with the given url.
77 When set to false, the connection is closed.
78 The default value is false.
82 \qmlsignal WebSocket::textMessageReceived(QString message)
83 This signal is emitted when a text message is received.
87 \qmlsignal WebSocket::statusChanged(Status status)
88 This signal is emitted when the status of the WebSocket changes.
89 the \l {WebSocket::status}{status} argument provides the current status.
95 \qmlmethod void WebSocket::sendTextMessage(string message)
96 Sends \c message to the server.
99 #include "qqmlwebsocket.h"
100 #include <QtWebSockets/QWebSocket>
104 QQmlWebSocket::QQmlWebSocket(QObject *parent) :
110 m_componentCompleted(true),
115 QQmlWebSocket::QQmlWebSocket(QWebSocket *socket, QObject *parent) :
118 m_url(socket->requestUrl()),
120 m_componentCompleted(true),
121 m_errorString(socket->errorString())
124 onStateChanged(socket->state());
127 QQmlWebSocket::~QQmlWebSocket()
131 qint64 QQmlWebSocket::sendTextMessage(const QString &message)
133 if (m_status != Open) {
134 setErrorString(tr("Messages can only be sent when the socket is open."));
138 return m_webSocket->sendTextMessage(message);
141 QUrl QQmlWebSocket::url() const
146 void QQmlWebSocket::setUrl(const QUrl &url)
151 if (m_webSocket && (m_status == Open)) {
152 m_webSocket->close();
157 m_webSocket->open(m_url);
161 QQmlWebSocket::Status QQmlWebSocket::status() const
166 QString QQmlWebSocket::errorString() const
168 return m_errorString;
171 void QQmlWebSocket::classBegin()
173 m_componentCompleted = false;
174 m_errorString = tr("QQmlWebSocket is not ready.");
178 void QQmlWebSocket::componentComplete()
180 setSocket(new QWebSocket);
182 m_componentCompleted = true;
187 void QQmlWebSocket::setSocket(QWebSocket *socket)
189 m_webSocket.reset(socket);
191 // explicit ownership via QScopedPointer
192 m_webSocket->setParent(Q_NULLPTR);
193 connect(m_webSocket.data(), &QWebSocket::textMessageReceived,
194 this, &QQmlWebSocket::textMessageReceived);
195 typedef void (QWebSocket::* ErrorSignal)(QAbstractSocket::SocketError);
196 connect(m_webSocket.data(), static_cast<ErrorSignal>(&QWebSocket::error),
197 this, &QQmlWebSocket::onError);
198 connect(m_webSocket.data(), &QWebSocket::stateChanged,
199 this, &QQmlWebSocket::onStateChanged);
203 void QQmlWebSocket::onError(QAbstractSocket::SocketError error)
206 setErrorString(m_webSocket->errorString());
210 void QQmlWebSocket::onStateChanged(QAbstractSocket::SocketState state)
214 case QAbstractSocket::ConnectingState:
215 case QAbstractSocket::BoundState:
216 case QAbstractSocket::HostLookupState:
218 setStatus(Connecting);
221 case QAbstractSocket::UnconnectedState:
226 case QAbstractSocket::ConnectedState:
231 case QAbstractSocket::ClosingState:
238 setStatus(Connecting);
244 void QQmlWebSocket::setStatus(QQmlWebSocket::Status status)
246 if (m_status == status) {
250 if (status != Error) {
253 Q_EMIT statusChanged(m_status);
256 void QQmlWebSocket::setActive(bool active)
258 if (m_isActive == active) {
262 Q_EMIT activeChanged(m_isActive);
263 if (!m_componentCompleted) {
273 bool QQmlWebSocket::isActive() const
278 void QQmlWebSocket::open()
280 if (m_componentCompleted && m_isActive && m_url.isValid() && Q_LIKELY(m_webSocket)) {
281 m_webSocket->open(m_url);
285 void QQmlWebSocket::close()
287 if (m_componentCompleted && Q_LIKELY(m_webSocket)) {
288 m_webSocket->close();
292 void QQmlWebSocket::setErrorString(QString errorString)
294 if (m_errorString == errorString) {
297 m_errorString = errorString;
298 Q_EMIT errorStringChanged(m_errorString);