QWebSocketServer

Implements a WebSocket-based server. More

Inheritance diagram of PySide6.QtWebSockets.QWebSocketServer

Synopsis

Functions

Virtual functions

Signals

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 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() .

class PySide6.QtWebSockets.QWebSocketServer(serverName, secureMode[, parent=None])
Parameters

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.

PySide6.QtWebSockets.QWebSocketServer.SslMode

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

Constant

Description

QWebSocketServer.SecureMode

The server operates in secure mode (over wss)

QWebSocketServer.NonSecureMode

The server operates in non-secure mode (over ws)

PySide6.QtWebSockets.QWebSocketServer.acceptError(socketError)
Parameters

socketErrorSocketError

PySide6.QtWebSockets.QWebSocketServer.alertReceived(level, type, description)
Parameters
PySide6.QtWebSockets.QWebSocketServer.alertSent(level, type, description)
Parameters
PySide6.QtWebSockets.QWebSocketServer.close()

Closes the server. The server will no longer listen for incoming connections.

PySide6.QtWebSockets.QWebSocketServer.closed()
PySide6.QtWebSockets.QWebSocketServer.error()
Return type

CloseCode

Returns an error code for the last error that occurred. If no error occurred, CloseCodeNormal is returned.

See also

errorString()

PySide6.QtWebSockets.QWebSocketServer.errorString()
Return type

str

Returns a human readable description of the last error that occurred. If no error occurred, an empty string is returned.

See also

serverError()

PySide6.QtWebSockets.QWebSocketServer.handleConnection(socket)
Parameters

socketPySide6.QtNetwork.QTcpSocket

Upgrades a tcp socket to websocket.

The QWebSocketServer object will take ownership of the socket object and delete it when appropriate.

PySide6.QtWebSockets.QWebSocketServer.handshakeInterruptedOnError(error)
Parameters

errorPySide6.QtNetwork.QSslError

PySide6.QtWebSockets.QWebSocketServer.handshakeTimeoutMS()
Return type

int

Returns the handshake timeout for new connections in milliseconds.

The default is 10 seconds. If a peer uses more time to complete the handshake their connection is closed.

See also

setHandshakeTimeout() handshakeTimeout()

PySide6.QtWebSockets.QWebSocketServer.hasPendingConnections()
Return type

bool

Returns true if the server has pending connections; otherwise returns false.

PySide6.QtWebSockets.QWebSocketServer.isListening()
Return type

bool

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() error()

PySide6.QtWebSockets.QWebSocketServer.listen([address=QHostAddress.Any[, port=0]])
Parameters
Return type

bool

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 Any , the server will listen on all network interfaces.

Returns true on success; otherwise returns false.

See also

isListening()

PySide6.QtWebSockets.QWebSocketServer.maxPendingConnections()
Return type

int

Returns the maximum number of pending accepted connections. The default is 30.

PySide6.QtWebSockets.QWebSocketServer.nativeDescriptor()
Return type

qintptr

Note

This function is deprecated.

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.

PySide6.QtWebSockets.QWebSocketServer.newConnection()
PySide6.QtWebSockets.QWebSocketServer.nextPendingConnection()
Return type

PySide6.QtWebSockets.QWebSocket

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.

PySide6.QtWebSockets.QWebSocketServer.originAuthenticationRequired(pAuthenticator)
Parameters

pAuthenticatorPySide6.QtWebSockets.QWebSocketCorsAuthenticator

PySide6.QtWebSockets.QWebSocketServer.pauseAccepting()

Pauses incoming new connections. Queued connections will remain in queue.

PySide6.QtWebSockets.QWebSocketServer.peerVerifyError(error)
Parameters

errorPySide6.QtNetwork.QSslError

PySide6.QtWebSockets.QWebSocketServer.preSharedKeyAuthenticationRequired(authenticator)
Parameters

authenticatorPySide6.QtNetwork.QSslPreSharedKeyAuthenticator

PySide6.QtWebSockets.QWebSocketServer.proxy()
Return type

PySide6.QtNetwork.QNetworkProxy

Returns the network proxy for this server. By default DefaultProxy is used.

See also

setProxy()

PySide6.QtWebSockets.QWebSocketServer.resumeAccepting()

Resumes accepting new connections.

See also

pauseAccepting()

PySide6.QtWebSockets.QWebSocketServer.secureMode()
Return type

SslMode

Returns the secure mode the server is running in.

See also

QWebSocketServer() SslMode

PySide6.QtWebSockets.QWebSocketServer.serverAddress()
Return type

PySide6.QtNetwork.QHostAddress

Returns the server’s address if the server is listening for connections; otherwise returns Null .

PySide6.QtWebSockets.QWebSocketServer.serverError(closeCode)
Parameters

closeCodeCloseCode

PySide6.QtWebSockets.QWebSocketServer.serverName()
Return type

str

Returns the server name that is used during the http handshake phase.

See also

setServerName()

PySide6.QtWebSockets.QWebSocketServer.serverPort()
Return type

quint16

Returns the server’s port if the server is listening for connections; otherwise returns 0.

PySide6.QtWebSockets.QWebSocketServer.serverUrl()
Return type

PySide6.QtCore.QUrl

Returns a URL clients can use to connect to this server if the server is listening for connections. Otherwise an invalid URL is returned.

PySide6.QtWebSockets.QWebSocketServer.setHandshakeTimeout(msec)
Parameters

msec – int

This is an overloaded function.

PySide6.QtWebSockets.QWebSocketServer.setMaxPendingConnections(numConnections)
Parameters

numConnections – int

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 CloseCodeAbnormalDisconnection close code when the maximum of connections has been reached. The WebSocket handshake will fail and the socket will be closed.

PySide6.QtWebSockets.QWebSocketServer.setNativeDescriptor(descriptor)
Parameters

descriptorqintptr

Return type

bool

Note

This function is deprecated.

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.

PySide6.QtWebSockets.QWebSocketServer.setProxy(networkProxy)
Parameters

networkProxyPySide6.QtNetwork.QNetworkProxy

Sets the explicit network proxy for this server to networkProxy.

To disable the use of a proxy, use the NoProxy proxy type:

server->setProxy(QNetworkProxy::NoProxy);

See also

proxy()

PySide6.QtWebSockets.QWebSocketServer.setServerName(serverName)
Parameters

serverName – str

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()

PySide6.QtWebSockets.QWebSocketServer.setSocketDescriptor(socketDescriptor)
Parameters

socketDescriptorqintptr

Return type

bool

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.

PySide6.QtWebSockets.QWebSocketServer.setSslConfiguration(sslConfiguration)
Parameters

sslConfigurationPySide6.QtNetwork.QSslConfiguration

Sets the SSL configuration for the QWebSocketServer to sslConfiguration. This method has no effect if QWebSocketServer runs in non-secure mode ( NonSecureMode ).

See also

sslConfiguration() SslMode

PySide6.QtWebSockets.QWebSocketServer.socketDescriptor()
Return type

qintptr

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.

PySide6.QtWebSockets.QWebSocketServer.sslConfiguration()
Return type

PySide6.QtNetwork.QSslConfiguration

Returns the SSL configuration used by the QWebSocketServer . If the server is not running in secure mode ( SecureMode ), this method returns defaultConfiguration() .

See also

setSslConfiguration() SslMode defaultConfiguration()

PySide6.QtWebSockets.QWebSocketServer.sslErrors(errors)
Parameters

errors

PySide6.QtWebSockets.QWebSocketServer.supportedVersions()
Return type

Returns a list of WebSocket versions that this server is supporting.