QWebSocketServer Class

Implements a WebSocket-based server. More...

Header: #include <QWebSocketServer>
qmake: QT += websockets
Since: Qt 5.3
Inherits: QObject

This class was introduced in Qt 5.3.

Public Types

enum SslMode { SecureMode, NonSecureMode }

Detailed Description

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.

There is a default connection handshake timeout of 10 seconds to avoid denial of service, which can be customized using setHandshakeTimeout().

See also WebSocket Server Example and QWebSocket.

Member Type Documentation

enum QWebSocketServer::SslMode

Indicates whether the server operates over wss (SecureMode) or ws (NonSecureMode)

ConstantValueDescription
QWebSocketServer::SecureMode0The server operates in secure mode (over wss)
QWebSocketServer::NonSecureMode1The server operates in non-secure mode (over ws)