QWebSocketServer¶
Implements a WebSocket-based server. More…
Synopsis¶
Functions¶
def
close
()def
error
()def
errorString
()def
handleConnection
(socket)def
handshakeTimeoutMS
()def
hasPendingConnections
()def
isListening
()def
listen
([address=QHostAddress.Any[, port=0]])def
maxPendingConnections
()def
nativeDescriptor
()def
pauseAccepting
()def
proxy
()def
resumeAccepting
()def
secureMode
()def
serverAddress
()def
serverName
()def
serverPort
()def
serverUrl
()def
setHandshakeTimeout
(msec)def
setMaxPendingConnections
(numConnections)def
setNativeDescriptor
(descriptor)def
setProxy
(networkProxy)def
setServerName
(serverName)def
setSocketDescriptor
(socketDescriptor)def
socketDescriptor
()def
supportedVersions
()
Virtual functions¶
def
nextPendingConnection
()
Signals¶
def
acceptError
(socketError)def
closed
()def
newConnection
()def
originAuthenticationRequired
(pAuthenticator)def
preSharedKeyAuthenticationRequired
(authenticator)def
serverError
(closeCode)
Detailed Description¶
It is modeled after
QTcpServer
, and behaves the same. So, if you know how to useQTcpServer
, you know how to useQWebSocketServer
. This class makes it possible to accept incoming WebSocket connections. You can specify the port or haveQWebSocketServer
pick one automatically. You can listen on a specific address or on all the machine’s addresses. Calllisten()
to have the server listen for incoming connections.The
newConnection()
signal is then emitted each time a client connects to the server. CallnextPendingConnection()
to accept the pending connection as a connectedQWebSocket
. The function returns a pointer to aQWebSocket
inConnectedState
that you can use for communicating with the client.If an error occurs,
serverError()
returns the type of error, anderrorString()
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()
andserverPort()
.Calling
close()
makesQWebSocketServer
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:
- param secureMode:
- param serverName:
str
Constructs a new
QWebSocketServer
with the givenserverName
. TheserverName
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. ThesecureMode
parameter indicates whether the server operates over wss (SecureMode
) or over ws (NonSecureMode
).parent
is passed to theQObject
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:
socketError –
SocketError
- 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:
Returns an error code for the last error that occurred. If no error occurred,
CloseCodeNormal
is returned.See also
- 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
- PySide2.QtWebSockets.QWebSocketServer.handleConnection(socket)¶
- Parameters:
socket –
PySide2.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.
- PySide2.QtWebSockets.QWebSocketServer.listen([address=QHostAddress.Any[, port=0]])¶
- Parameters:
address –
PySide2.QtNetwork.QHostAddress
port –
quint16
- Return type:
bool
Tells the server to listen for incoming connections on address
address
and portport
. Ifport
is 0, a port is chosen automatically. Ifaddress
isAny
, the server will listen on all network interfaces.Returns true on success; otherwise returns false.
See also
- 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.See also
- PySide2.QtWebSockets.QWebSocketServer.newConnection()¶
- PySide2.QtWebSockets.QWebSocketServer.nextPendingConnection()¶
- Return type:
Returns the next pending connection as a connected
QWebSocket
object.QWebSocketServer
does not take ownership of the returnedQWebSocket
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
- PySide2.QtWebSockets.QWebSocketServer.originAuthenticationRequired(pAuthenticator)¶
- Parameters:
pAuthenticator –
PySide2.QtWebSockets.QWebSocketCorsAuthenticator
- PySide2.QtWebSockets.QWebSocketServer.pauseAccepting()¶
Pauses incoming new connections. Queued connections will remain in queue.
See also
- Parameters:
authenticator –
PySide2.QtNetwork.QSslPreSharedKeyAuthenticator
- PySide2.QtWebSockets.QWebSocketServer.proxy()¶
- Return type:
Returns the network proxy for this server. By default
DefaultProxy
is used.See also
- PySide2.QtWebSockets.QWebSocketServer.resumeAccepting()¶
Resumes accepting new connections.
See also
- PySide2.QtWebSockets.QWebSocketServer.secureMode()¶
- Return type:
Returns the secure mode the server is running in.
See also
QWebSocketServer()
SslMode
- PySide2.QtWebSockets.QWebSocketServer.serverAddress()¶
- Return type:
Returns the server’s address if the server is listening for connections; otherwise returns
Null
.See also
- PySide2.QtWebSockets.QWebSocketServer.serverName()¶
- Return type:
str
Returns the server name that is used during the http handshake phase.
See also
- PySide2.QtWebSockets.QWebSocketServer.serverPort()¶
- Return type:
quint16
Returns the server’s port if the server is listening for connections; otherwise returns 0.
See also
- PySide2.QtWebSockets.QWebSocketServer.serverUrl()¶
- Return type:
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
- 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 thannumConnections
incoming connections beforenextPendingConnection()
is called. By default, the limit is 30 pending connections.QWebSocketServer
will emit theerror()
signal with theCloseCodeAbnormalDisconnection
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:
descriptor –
qintptr
- 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.
See also
- PySide2.QtWebSockets.QWebSocketServer.setProxy(networkProxy)¶
- Parameters:
networkProxy –
PySide2.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
- 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
. TheserverName
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
- 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.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.