tufao
1.3.0
An asynchronous web framework for C++ built on top of Qt
|
This class represents a WebSocket connection. More...
Public Slots | |
void | close () override |
bool | sendMessage (const QByteArray &msg) override |
bool | sendBinaryMessage (const QByteArray &msg) |
Sends a binary message over the connection. More... | |
bool | sendUtf8Message (const QByteArray &msg) |
Sends a UTF-8 text message over the connection. More... | |
bool | ping (const QByteArray &data) |
Sends a ping frame over the connection. More... | |
Signals | |
void | pong (QByteArray data) |
This signal is emitted when a pong frame is received. More... | |
Public Member Functions | |
WebSocket (QObject *parent=0) | |
Constructs a Tufao::WebSocket object. More... | |
~WebSocket () | |
Destroys the object. | |
bool | connectToHost (const QHostAddress &address, quint16 port, const QByteArray &resource, const Headers &headers=Headers()) |
Execute the steps to establish a WebSocket connection with the server represented by address and port on the given resource . More... | |
bool | connectToHost (const QHostAddress &address, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
bool | connectToHost (const QString &hostname, quint16 port, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
bool | connectToHost (const QString &hostname, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
bool | connectToHostEncrypted (const QString &hostname, quint16 port, const QByteArray &resource, const Headers &headers, const QList< QSslError > &ignoredSslErrors) |
The same as WebSocket::connectToHost, but uses a TLS connection. More... | |
bool | connectToHostEncrypted (const QString &hostname, quint16 port, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. | |
bool | connectToHostEncrypted (const QString &hostname, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
bool | connectToHostEncrypted (const QHostAddress &address, quint16 port, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
bool | connectToHostEncrypted (const QHostAddress &address, const QByteArray &resource, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
bool | startServerHandshake (const HttpServerRequest &request, const QByteArray &head, const Headers &headers=Headers()) |
It establish a WebSocket connection initiated by request with head data. More... | |
bool | startServerHandshake (HttpServerRequest &request, const Headers &headers=Headers()) |
This is an overloaded function. More... | |
void | setMessagesType (WebSocketMessageType type) |
Set the type of messages sent through WebSocket::sendMessage method. More... | |
WebSocketMessageType | messagesType () const |
Return the current type of messages what will be sent through WebSocket::sendMessage method. More... | |
WebSocketError | error () const |
Returns the type of last error that occurred. | |
QString | errorString () const |
Returns a human-readable description of the last error that occurred. | |
QHostAddress | peerAddress () const |
Returns the address of the connected peer. More... | |
quint16 | peerPort () const |
Returns the port of the connected peer. More... | |
Static Public Member Functions | |
static QList< QByteArray > | supportedProtocols (const Headers &headers) |
It reads the supported sub-protocols from the appropriate fields and return them. | |
Additional Inherited Members |
This class represents a WebSocket connection.
WebSocket is a protocol designed to allow HTTP user agents and servers communicates using a two-way protocol. It's possible to upgrade an established HTTP connection to a WebSocket connection.
If you intend to create a server able to accept WebSocket connections, you must create a HTTP server and create a handler to upgrade events.
In the handler for the upgrade event, you must create a new WebSocket object and call the method startServerHandshake. This method will send the initial WebSocket server payload and check if it's a valid WebSocket connection request.
If you intend to connect to a WebSocket server, you must call one of the connectToHost methods. If the connection should be encrypted, then call one of the connectToHostEncrypted methods. The connected signal will emitted when the socket is ready.
|
explicit |
Constructs a Tufao::WebSocket object.
parent
is passed to the QObject constructor.
bool Tufao::WebSocket::connectToHost | ( | const QHostAddress & | address, |
quint16 | port, | ||
const QByteArray & | resource, | ||
const Headers & | headers = Headers() |
||
) |
Execute the steps to establish a WebSocket connection with the server represented by address
and port
on the given resource
.
You can send extra headers in the WebSocket opening handshake request using the headers
argument.
bool Tufao::WebSocket::connectToHost | ( | const QHostAddress & | address, |
const QByteArray & | resource, | ||
const Headers & | headers = Headers() |
||
) |
This is an overloaded function.
It uses port 80 to establish the connection.
bool Tufao::WebSocket::connectToHost | ( | const QString & | hostname, |
quint16 | port, | ||
const QByteArray & | resource, | ||
const Headers & | headers = Headers() |
||
) |
This is an overloaded function.
bool Tufao::WebSocket::connectToHost | ( | const QString & | hostname, |
const QByteArray & | resource, | ||
const Headers & | headers = Headers() |
||
) |
This is an overloaded function.
It uses port 80 to establish the connection.
bool Tufao::WebSocket::connectToHostEncrypted | ( | const QString & | hostname, |
quint16 | port, | ||
const QByteArray & | resource, | ||
const Headers & | headers, | ||
const QList< QSslError > & | ignoredSslErrors | ||
) |
The same as WebSocket::connectToHost, but uses a TLS connection.
ignoredSslErrors
is passed to QSslSocket::ignoreSslErrors.
bool Tufao::WebSocket::connectToHostEncrypted | ( | const QString & | hostname, |
const QByteArray & | resource, | ||
const Headers & | headers = Headers() |
||
) |
This is an overloaded function.
It uses port 443 to establish the connection.
bool Tufao::WebSocket::connectToHostEncrypted | ( | const QHostAddress & | address, |
const QByteArray & | resource, | ||
const Headers & | headers = Headers() |
||
) |
This is an overloaded function.
It uses port 443 to establish the connection.
WebSocketMessageType Tufao::WebSocket::messagesType | ( | ) | const |
Return the current type of messages what will be sent through WebSocket::sendMessage method.
QHostAddress Tufao::WebSocket::peerAddress | ( | ) | const |
Returns the address of the connected peer.
QHostAddress::Null | if the socket is not in ConnectedState. |
quint16 Tufao::WebSocket::peerPort | ( | ) | const |
Returns the port of the connected peer.
0 | if the socket is not in ConnectedState. |
|
slot |
Sends a ping frame over the connection.
A WebSocket peer that receives a ping frame must respond with a pong frame as soon as pratical.
You can use a ping frame to measure the connection lag, to test the connectivity availability, among other.
|
signal |
This signal is emitted when a pong frame is received.
Pong frames are sent in response to ping frames.
|
slot |
Sends a binary message over the connection.
|
slot |
Sends a UTF-8 text message over the connection.
void Tufao::WebSocket::setMessagesType | ( | WebSocketMessageType | type | ) |
Set the type of messages sent through WebSocket::sendMessage method.
bool Tufao::WebSocket::startServerHandshake | ( | const HttpServerRequest & | request, |
const QByteArray & | head, | ||
const Headers & | headers = Headers() |
||
) |
It establish a WebSocket connection initiated by request
with head
data.
The method send the initial WebSocket server payload and check if it's a valid WebSocket connection request.
request
emitts the Tufao::HttpServerRequest::upgrade signal.request
. If you want to respond to these headers in the opening handshake response, just put the headers you want to send in the response in the headers
argument. Some headers you may want to treat are:bool Tufao::WebSocket::startServerHandshake | ( | HttpServerRequest & | request, |
const Headers & | headers = Headers() |
||
) |
This is an overloaded function.
It uses HttpServerRequest::readBody to figure out the head argument.