QWebSocketServer

Implements a WebSocket-based server. More

Inheritance diagram of PySide2.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() .

See also

WebSocket Server Example QWebSocket

class PySide2.QtWebSockets.QWebSocketServer(serverName, secureMode[, parent=None])
param parent

PySide2.QtCore.QObject

param secureMode

SslMode

param serverName

str

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.

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

PySide2.QtWebSockets.QWebSocketServer.acceptError(socketError)
Parameters

socketErrorSocketError

PySide2.QtWebSockets.QWebSocketServer.close()

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

PySide2.QtWebSockets.QWebSocketServer.closed()
PySide2.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()

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

PySide2.QtWebSockets.QWebSocketServer.handleConnection(socket)
Parameters

socketPySide2.QtNetwork.QTcpSocket

Upgrades a tcp socket to websocket.

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

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

PySide2.QtWebSockets.QWebSocketServer.hasPendingConnections()
Return type

bool

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

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

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

PySide2.QtWebSockets.QWebSocketServer.maxPendingConnections()
Return type

int

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

PySide2.QtWebSockets.QWebSocketServer.nativeDescriptor()
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.

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

PySide2.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.

PySide2.QtWebSockets.QWebSocketServer.originAuthenticationRequired(pAuthenticator)
Parameters

pAuthenticatorPySide2.QtWebSockets.QWebSocketCorsAuthenticator

PySide2.QtWebSockets.QWebSocketServer.pauseAccepting()

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

PySide2.QtWebSockets.QWebSocketServer.preSharedKeyAuthenticationRequired(authenticator)
Parameters

authenticatorPySide2.QtNetwork.QSslPreSharedKeyAuthenticator

PySide2.QtWebSockets.QWebSocketServer.proxy()
Return type

PySide2.QtNetwork.QNetworkProxy

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

See also

setProxy()

PySide2.QtWebSockets.QWebSocketServer.resumeAccepting()

Resumes accepting new connections.

See also

pauseAccepting()

PySide2.QtWebSockets.QWebSocketServer.secureMode()
Return type

SslMode

Returns the secure mode the server is running in.

See also

QWebSocketServer() SslMode

PySide2.QtWebSockets.QWebSocketServer.serverAddress()
Return type

PySide2.QtNetwork.QHostAddress

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

PySide2.QtWebSockets.QWebSocketServer.serverError(closeCode)
Parameters

closeCodeCloseCode

PySide2.QtWebSockets.QWebSocketServer.serverName()
Return type

str

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

See also

setServerName()

PySide2.QtWebSockets.QWebSocketServer.serverPort()
Return type

quint16

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

PySide2.QtWebSockets.QWebSocketServer.serverUrl()
Return type

PySide2.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.

PySide2.QtWebSockets.QWebSocketServer.setHandshakeTimeout(msec)
Parameters

msec – int

This is an overloaded function.

PySide2.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.

PySide2.QtWebSockets.QWebSocketServer.setNativeDescriptor(descriptor)
Parameters

descriptorqintptr

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.

PySide2.QtWebSockets.QWebSocketServer.setProxy(networkProxy)
Parameters

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

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

PySide2.QtWebSockets.QWebSocketServer.setSocketDescriptor(socketDescriptor)
Parameters

socketDescriptor – int

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.

PySide2.QtWebSockets.QWebSocketServer.socketDescriptor()
Return type

int

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.

PySide2.QtWebSockets.QWebSocketServer.supportedVersions()
Return type

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