Added check on QT_NO_NETWORKPROXY to include proxy functionality only when required
[contrib/qtwebsockets.git] / src / qwebsocket.h
1 /**
2  * @file websocket.h
3  * @brief Defines the WebSocket class.
4  *
5  * \note Currently, only V13 (RFC6455) is supported.
6  * \note Both text and binary websockets are supported.
7  * \note The secure version (wss) is currently not implemented.
8  * @author Kurt Pattyn (pattyn.kurt@gmail.com)
9  */
10
11 #ifndef QWEBSOCKET_H
12 #define QWEBSOCKET_H
13
14 #include <QUrl>
15 #include <QAbstractSocket>
16 #include <QHostAddress>
17 #ifndef QT_NO_NETWORKPROXY
18 #include <QNetworkProxy>
19 #endif
20 #include <QTime>
21 #include "qwebsocketsglobal.h"
22 #include "qwebsocketprotocol.h"
23
24 QT_BEGIN_NAMESPACE
25
26 class QTcpSocket;
27 class QWebSocketPrivate;
28
29 class Q_WEBSOCKETS_EXPORT QWebSocket:public QObject
30 {
31         Q_OBJECT
32
33 public:
34         explicit QWebSocket(QString origin = QString(), QWebSocketProtocol::Version version = QWebSocketProtocol::V_LATEST, QObject *parent = 0);
35         virtual ~QWebSocket();
36
37         void abort();
38         QAbstractSocket::SocketError error() const;
39         QString errorString() const;
40         bool flush();
41         bool isValid();
42         QHostAddress localAddress() const;
43         quint16 localPort() const;
44         QHostAddress peerAddress() const;
45         QString peerName() const;
46         quint16 peerPort() const;
47 #ifndef QT_NO_NETWORKPROXY
48         QNetworkProxy proxy() const;
49         void setProxy(const QNetworkProxy &networkProxy);
50 #endif
51         qint64 readBufferSize() const;
52         void setReadBufferSize(qint64 size);
53         void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
54         QVariant socketOption(QAbstractSocket::SocketOption option);
55         QAbstractSocket::SocketState state() const;
56
57         bool waitForConnected(int msecs = 30000);
58         bool waitForDisconnected(int msecs = 30000);
59
60         QWebSocketProtocol::Version version();
61         QString resourceName();
62         QUrl requestUrl();
63         QString origin();
64         QString protocol();
65         QString extension();
66
67         qint64 write(const char *message);              //send data as text
68         qint64 write(const char *message, qint64 maxSize);              //send data as text
69         qint64 write(const QString &message);   //send data as text
70         qint64 write(const QByteArray &data);   //send data as binary
71
72 public Q_SLOTS:
73         virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
74         virtual void open(const QUrl &url, bool mask = true);
75         void ping();
76
77 Q_SIGNALS:
78         void aboutToClose();
79         void connected();
80         void disconnected();
81         void stateChanged(QAbstractSocket::SocketState state);
82 #ifndef QT_NO_NETWORKPROXY
83         void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
84 #endif
85         void readChannelFinished();
86         void textFrameReceived(QString frame, bool isLastFrame);
87         void binaryFrameReceived(QByteArray frame, bool isLastFrame);
88         void textMessageReceived(QString message);
89         void binaryMessageReceived(QByteArray message);
90         void error(QAbstractSocket::SocketError error);
91         void pong(quint64 elapsedTime);
92
93 private:
94         Q_DISABLE_COPY(QWebSocket)
95         QWebSocket(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QObject *parent = 0);
96         QWebSocketPrivate * const d_ptr;
97
98         friend class QWebSocketPrivate;
99 };
100
101 QT_END_NAMESPACE
102
103 #endif // QWEBSOCKET_H