QWebSocketServer Class
Implements a WebSocket-based server. More...
Header: | #include <QWebSocketServer> |
qmake: | QT += websockets |
Since: | Qt 5.3 |
Inherits: | QObject |
Public Types
enum | SslMode { SecureMode, NonSecureMode } |
Public Functions
QWebSocketServer(const QString &serverName, QWebSocketServer::SslMode secureMode, QObject *parent = nullptr) | |
virtual | ~QWebSocketServer() |
void | close() |
QWebSocketProtocol::CloseCode | error() const |
QString | errorString() const |
void | handleConnection(QTcpSocket *socket) const |
bool | hasPendingConnections() const |
bool | isListening() const |
bool | listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0) |
int | maxPendingConnections() const |
virtual QWebSocket * | nextPendingConnection() |
void | pauseAccepting() |
QNetworkProxy | proxy() const |
void | resumeAccepting() |
QWebSocketServer::SslMode | secureMode() const |
QHostAddress | serverAddress() const |
QString | serverName() const |
quint16 | serverPort() const |
QUrl | serverUrl() const |
void | setMaxPendingConnections(int numConnections) |
void | setProxy(const QNetworkProxy &networkProxy) |
void | setServerName(const QString &serverName) |
bool | setSocketDescriptor(int socketDescriptor) |
void | setSslConfiguration(const QSslConfiguration &sslConfiguration) |
int | socketDescriptor() const |
QSslConfiguration | sslConfiguration() const |
QList<QWebSocketProtocol::Version> | supportedVersions() const |
- 34 public functions inherited from QObject
Signals
void | acceptError(QAbstractSocket::SocketError socketError) |
void | closed() |
void | newConnection() |
void | originAuthenticationRequired(QWebSocketCorsAuthenticator *pAuthenticator) |
void | peerVerifyError(const int &error) |
void | preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator) |
void | serverError(QWebSocketProtocol::CloseCode closeCode) |
void | sslErrors(const int &errors) |
- 2 signals inherited from QObject
Additional Inherited Members
- 1 property inherited from QObject
- 1 public slot inherited from QObject
- 1 public variable inherited from QObject
- 10 static public members inherited from QObject
- 9 protected functions inherited from QObject
- 2 protected variables inherited from QObject
Detailed Description
Implements a WebSocket-based server.
It is modeled after QTcpServer, and behaves the same. So, if you know how to use QTcpServer, you know how to use QWebSocketServer. This class makes it possible to accept incoming WebSocket connections. You can specify the port or have QWebSocketServer pick one automatically. You can listen on a specific address or on all the machine's addresses. Call listen() to have the server listen for incoming connections.
The newConnection() signal is then emitted each time a client connects to the server. Call nextPendingConnection() to accept the pending connection as a connected QWebSocket. The function returns a pointer to a QWebSocket in QAbstractSocket::ConnectedState that you can use for communicating with the client.
If an error occurs, serverError() returns the type of error, and errorString() can be called to get a human readable description of what happened.
When listening for connections, the address and port on which the server is listening are available as serverAddress() and serverPort().
Calling close() makes QWebSocketServer stop listening for incoming connections.
QWebSocketServer currently does not support WebSocket Extensions and WebSocket Subprotocols.
Note: When working with self-signed certificates, Firefox bug 594502 prevents Firefox to connect to a secure WebSocket server. To work around this problem, first browse to the secure WebSocket server using HTTPS. FireFox will indicate that the certificate is invalid. From here on, the certificate can be added to the exceptions. After this, the secure WebSockets connection should work.
QWebSocketServer only supports version 13 of the WebSocket protocol, as outlined in RFC 6455.
See also WebSocket Server Example and QWebSocket.
Member Type Documentation
enum QWebSocketServer::SslMode
Indicates whether the server operates over wss (SecureMode) or ws (NonSecureMode)
Constant | Value | Description |
---|---|---|
QWebSocketServer::SecureMode | 0 | The server operates in secure mode (over wss) |
QWebSocketServer::NonSecureMode | 1 | The server operates in non-secure mode (over ws) |
Member Function Documentation
QWebSocketServer::QWebSocketServer(const QString &serverName, QWebSocketServer::SslMode secureMode, QObject *parent = nullptr)
Constructs a new QWebSocketServer with the given serverName. The serverName will be used in the HTTP handshake phase to identify the server. It can be empty, in which case no server name will be sent to the client. The secureMode parameter indicates whether the server operates over wss (SecureMode) or over ws (NonSecureMode).
parent is passed to the QObject constructor.
[virtual]
QWebSocketServer::~QWebSocketServer()
Destroys the QWebSocketServer object. If the server is listening for connections, the socket is automatically closed. Any client QWebSockets that are still queued are closed and deleted.
See also close().
[signal]
void QWebSocketServer::acceptError(QAbstractSocket::SocketError socketError)
void QWebSocketServer::close()
Closes the server. The server will no longer listen for incoming connections.
[signal]
void QWebSocketServer::closed()
QWebSocketProtocol::CloseCode QWebSocketServer::error() const
Returns an error code for the last error that occurred. If no error occurred, QWebSocketProtocol::CloseCodeNormal is returned.
See also errorString().
QString QWebSocketServer::errorString() const
Returns a human readable description of the last error that occurred. If no error occurred, an empty string is returned.
See also serverError().
void QWebSocketServer::handleConnection(QTcpSocket *socket) const
Upgrades a tcp socket to websocket.
The QWebSocketServer object will take ownership of the socket object and delete it when appropriate.
This function was introduced in Qt 5.9.
bool QWebSocketServer::hasPendingConnections() const
Returns true if the server has pending connections; otherwise returns false.
See also nextPendingConnection() and setMaxPendingConnections().
bool QWebSocketServer::isListening() const
Returns true if the server is currently listening for incoming connections; otherwise returns false. If listening fails, error() will return the reason.
See also listen() and error().
bool QWebSocketServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
Tells the server to listen for incoming connections on address address and port port. If port is 0, a port is chosen automatically. If address is QHostAddress::Any, the server will listen on all network interfaces.
Returns true on success; otherwise returns false.
See also isListening().
int QWebSocketServer::maxPendingConnections() const
Returns the maximum number of pending accepted connections. The default is 30.
See also setMaxPendingConnections() and hasPendingConnections().
[signal]
void QWebSocketServer::newConnection()
[virtual]
QWebSocket *QWebSocketServer::nextPendingConnection()
Returns the next pending connection as a connected QWebSocket object. QWebSocketServer does not take ownership of the returned QWebSocket object. It is up to the caller to delete the object explicitly when it will no longer be used, otherwise a memory leak will occur. nullptr is returned if this function is called when there are no pending connections.
Note: The returned QWebSocket object cannot be used from another thread.
See also hasPendingConnections().
[signal]
void QWebSocketServer::originAuthenticationRequired(QWebSocketCorsAuthenticator *pAuthenticator)
void QWebSocketServer::pauseAccepting()
Pauses incoming new connections. Queued connections will remain in queue.
See also resumeAccepting().
[signal]
void QWebSocketServer::peerVerifyError(const int &error)
[signal]
void QWebSocketServer::preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator)
QNetworkProxy QWebSocketServer::proxy() const
Returns the network proxy for this server. By default QNetworkProxy::DefaultProxy is used.
See also setProxy().
void QWebSocketServer::resumeAccepting()
Resumes accepting new connections.
See also pauseAccepting().
QWebSocketServer::SslMode QWebSocketServer::secureMode() const
Returns the secure mode the server is running in.
See also QWebSocketServer() and SslMode.
QHostAddress QWebSocketServer::serverAddress() const
Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null.
See also serverPort() and listen().
[signal]
void QWebSocketServer::serverError(QWebSocketProtocol::CloseCode closeCode)
QString QWebSocketServer::serverName() const
Returns the server name that is used during the http handshake phase.
See also setServerName().
quint16 QWebSocketServer::serverPort() const
Returns the server's port if the server is listening for connections; otherwise returns 0.
See also serverAddress() and listen().
QUrl QWebSocketServer::serverUrl() const
Returns a URL clients can use to connect to this server if the server is listening for connections. Otherwise an invalid URL is returned.
See also serverPort(), serverAddress(), and listen().
void QWebSocketServer::setMaxPendingConnections(int numConnections)
Sets the maximum number of pending accepted connections to numConnections. WebSocketServer will accept no more than numConnections incoming connections before nextPendingConnection() is called. By default, the limit is 30 pending connections.
QWebSocketServer will emit the error() signal with the QWebSocketProtocol::CloseCodeAbnormalDisconnection close code when the maximum of connections has been reached. The WebSocket handshake will fail and the socket will be closed.
See also maxPendingConnections() and hasPendingConnections().
void QWebSocketServer::setProxy(const QNetworkProxy &networkProxy)
Sets the explicit network proxy for this server to networkProxy.
To disable the use of a proxy, use the QNetworkProxy::NoProxy proxy type:
server->setProxy(QNetworkProxy::NoProxy);
See also proxy().
void QWebSocketServer::setServerName(const QString &serverName)
Sets the server name that will be used during the HTTP handshake phase to the given serverName. The serverName can be empty, in which case an empty server name will be sent to the client. Existing connected clients will not be notified of this change, only newly connecting clients will see this new name.
See also serverName().
bool QWebSocketServer::setSocketDescriptor(int socketDescriptor)
Sets the socket descriptor this server should use when listening for incoming connections to socketDescriptor.
Returns true if the socket is set successfully; otherwise returns false. The socket is assumed to be in listening state.
See also socketDescriptor() and isListening().
void QWebSocketServer::setSslConfiguration(const QSslConfiguration &sslConfiguration)
Sets the SSL configuration for the QWebSocketServer to sslConfiguration. This method has no effect if QWebSocketServer runs in non-secure mode (QWebSocketServer::NonSecureMode).
See also sslConfiguration() and SslMode.
int QWebSocketServer::socketDescriptor() const
Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening. If the server is using QNetworkProxy, the returned descriptor may not be usable with native socket functions.
See also setSocketDescriptor() and isListening().
QSslConfiguration QWebSocketServer::sslConfiguration() const
Returns the SSL configuration used by the QWebSocketServer. If the server is not running in secure mode (QWebSocketServer::SecureMode), this method returns QSslConfiguration::defaultConfiguration().
See also setSslConfiguration(), SslMode, and QSslConfiguration::defaultConfiguration().
[signal]
void QWebSocketServer::sslErrors(const int &errors)
QList<QWebSocketProtocol::Version> QWebSocketServer::supportedVersions() const
Returns a list of WebSocket versions that this server is supporting.