1/****************************************************************************
2**
3** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWebSockets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLWEBSOCKET_H
41#define QQMLWEBSOCKET_H
42
43#include <QObject>
44#include <QQmlParserStatus>
45#include <QtQml>
46#include <QScopedPointer>
47#include <QtWebSockets/QWebSocket>
48
49QT_BEGIN_NAMESPACE
50
51class QQmlWebSocket : public QObject, public QQmlParserStatus
52{
53 Q_OBJECT
54 Q_DISABLE_COPY(QQmlWebSocket)
55 Q_INTERFACES(QQmlParserStatus)
56
57 Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
58 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
59 Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
60 Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
61
62public:
63 explicit QQmlWebSocket(QObject *parent = 0);
64 explicit QQmlWebSocket(QWebSocket *socket, QObject *parent = 0);
65 ~QQmlWebSocket() override;
66
67 enum Status
68 {
69 Connecting = 0,
70 Open = 1,
71 Closing = 2,
72 Closed = 3,
73 Error = 4
74 };
75 Q_ENUM(Status)
76
77 QUrl url() const;
78 void setUrl(const QUrl &url);
79 Status status() const;
80 QString errorString() const;
81
82 void setActive(bool active);
83 bool isActive() const;
84
85 Q_INVOKABLE qint64 sendTextMessage(const QString &message);
86 Q_REVISION(1) Q_INVOKABLE qint64 sendBinaryMessage(const QByteArray &message);
87
88Q_SIGNALS:
89 void textMessageReceived(QString message);
90 Q_REVISION(1) void binaryMessageReceived(QByteArray message);
91 void statusChanged(QQmlWebSocket::Status status);
92 void activeChanged(bool isActive);
93 void errorStringChanged(QString errorString);
94 void urlChanged();
95
96public:
97 void classBegin() override;
98 void componentComplete() override;
99
100private Q_SLOTS:
101 void onError(QAbstractSocket::SocketError error);
102 void onStateChanged(QAbstractSocket::SocketState state);
103
104private:
105 QScopedPointer<QWebSocket> m_webSocket;
106 Status m_status;
107 QUrl m_url;
108 bool m_isActive;
109 bool m_componentCompleted;
110 QString m_errorString;
111
112 // takes ownership of the socket
113 void setSocket(QWebSocket *socket);
114 void setStatus(Status status);
115 void open();
116 void close();
117 void setErrorString(QString errorString = QString());
118};
119
120QT_END_NAMESPACE
121
122#endif // QQMLWEBSOCKET_H
123

source code of qtwebsockets/src/imports/qmlwebsockets/qqmlwebsocket.h