QWebSocket¶
Implements a TCP socket that talks the WebSocket protocol. More…
Synopsis¶
Functions¶
def
abort
()def
bytesToWrite
()def
closeCode
()def
closeReason
()def
error
()def
errorString
()def
flush
()def
isValid
()def
localAddress
()def
localPort
()def
maskGenerator
()def
maxAllowedIncomingFrameSize
()def
origin
()def
outgoingFrameSize
()def
pauseMode
()def
peerAddress
()def
peerName
()def
peerPort
()def
proxy
()def
readBufferSize
()def
request
()def
requestUrl
()def
resourceName
()def
resume
()def
sendBinaryMessage
(data)def
sendTextMessage
(message)def
setMaskGenerator
(maskGenerator)def
setMaxAllowedIncomingFrameSize
(maxAllowedIncomingFrameSize)def
setMaxAllowedIncomingMessageSize
(maxAllowedIncomingMessageSize)def
setOutgoingFrameSize
(outgoingFrameSize)def
setPauseMode
(pauseMode)def
setProxy
(networkProxy)def
setReadBufferSize
(size)def
state
()def
version
()
Slots¶
Signals¶
def
aboutToClose
()def
binaryFrameReceived
(frame, isLastFrame)def
binaryMessageReceived
(message)def
bytesWritten
(bytes)def
connected
()def
disconnected
()def
error
(error)def
pong
(elapsedTime, payload)def
preSharedKeyAuthenticationRequired
(authenticator)def
proxyAuthenticationRequired
(proxy, pAuthenticator)def
readChannelFinished
()def
stateChanged
(state)def
textFrameReceived
(frame, isLastFrame)def
textMessageReceived
(message)
Static functions¶
def
maxIncomingFrameSize
()def
maxIncomingMessageSize
()def
maxOutgoingFrameSize
()
Detailed Description¶
WebSockets is a web technology providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011.
QWebSocket
can both be used in a client application and server application.This class was modeled after
QAbstractSocket
.
QWebSocket
currently does not support WebSocket Extensions and WebSocket Subprotocols .
QWebSocket
only supports version 13 of the WebSocket protocol, as outlined in RFC 6455 .Note
Some proxies do not understand certain HTTP headers used during a WebSocket handshake. In that case, non-secure WebSocket connections fail. The best way to mitigate against this problem is to use WebSocket over a secure connection.
Warning
To generate masks, this implementation of WebSockets uses the reasonably secure
global()
->generate() function. For more information about the importance of good masking, see “Talking to Yourself for Fun and Profit” by Lin-Shung Huang et al . The best measure against attacks mentioned in the document above, is to useQWebSocket
over a secure connection (wss:// ). In general, always be careful to not have 3rd party script access to aQWebSocket
in your application.See also
QAbstractSocket
QTcpSocket
QWebSocket client example
- class PySide2.QtWebSockets.QWebSocket([origin=""[, version=QWebSocketProtocol.VersionLatest[, parent=None]]])¶
- param parent:
- param origin:
str
- param version:
Creates a new
QWebSocket
with the givenorigin
, theversion
of the protocol to use andparent
.The
origin
of the client is as specified in RFC 6454 . (Theorigin
is not required for non-web browser clients (see RFC 6455 )). Theorigin
may not contain new line characters, otherwise the connection will be aborted immediately during the handshake phase.Note
Currently only V13 ( RFC 6455 ) is supported
- PySide2.QtWebSockets.QWebSocket.abort()¶
Aborts the current socket and resets the socket. Unlike
close()
, this function immediately closes the socket, discarding any pending data in the write buffer.
- PySide2.QtWebSockets.QWebSocket.aboutToClose()¶
- PySide2.QtWebSockets.QWebSocket.binaryFrameReceived(frame, isLastFrame)¶
- Parameters:
frame –
PySide2.QtCore.QByteArray
isLastFrame – bool
- PySide2.QtWebSockets.QWebSocket.binaryMessageReceived(message)¶
- Parameters:
message –
PySide2.QtCore.QByteArray
- PySide2.QtWebSockets.QWebSocket.bytesToWrite()¶
- Return type:
int
Returns the number of bytes that are waiting to be written. The bytes are written when control goes back to the event loop or when
flush()
is called.See also
- PySide2.QtWebSockets.QWebSocket.bytesWritten(bytes)¶
- Parameters:
bytes – int
- PySide2.QtWebSockets.QWebSocket.close([closeCode=QWebSocketProtocol.CloseCodeNormal[, reason=""]])¶
- Parameters:
closeCode –
CloseCode
reason – str
Gracefully closes the socket with the given
closeCode
andreason
.Any data in the write buffer is flushed before the socket is closed. The
closeCode
is aCloseCode
indicating the reason to close, andreason
describes the reason of the closure more in detail. All control frames, including the Close frame, are limited to 125 bytes. Since two of these are used forcloseCode
the maximum length ofreason
is 123! Ifreason
exceeds this limit it will be truncated.
- PySide2.QtWebSockets.QWebSocket.closeCode()¶
- Return type:
Returns the code indicating why the socket was closed.
See also
CloseCode
closeReason()
- PySide2.QtWebSockets.QWebSocket.closeReason()¶
- Return type:
str
Returns the reason why the socket was closed.
See also
- PySide2.QtWebSockets.QWebSocket.connected()¶
- PySide2.QtWebSockets.QWebSocket.disconnected()¶
- PySide2.QtWebSockets.QWebSocket.error()¶
- Return type:
Returns the type of error that last occurred
See also
- PySide2.QtWebSockets.QWebSocket.error(error)
- Parameters:
error –
SocketError
- PySide2.QtWebSockets.QWebSocket.errorString()¶
- Return type:
str
Returns a human-readable description of the last error that occurred
See also
- PySide2.QtWebSockets.QWebSocket.flush()¶
- Return type:
bool
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned. Call this function if you need
QWebSocket
to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, becauseQWebSocket
will start sending data automatically once control goes back to the event loop.
- PySide2.QtWebSockets.QWebSocket.isValid()¶
- Return type:
bool
Returns
true
if the socket is ready for reading and writing; otherwise returnsfalse
.
- PySide2.QtWebSockets.QWebSocket.localAddress()¶
- Return type:
Returns the local address
- PySide2.QtWebSockets.QWebSocket.localPort()¶
- Return type:
quint16
Returns the local port
- PySide2.QtWebSockets.QWebSocket.maskGenerator()¶
- Return type:
Returns the mask generator that is currently used by this
QWebSocket
.See also
- PySide2.QtWebSockets.QWebSocket.maxAllowedIncomingFrameSize()¶
- Return type:
int
Returns the maximum allowed size of an incoming websocket frame.
See also
- PySide2.QtWebSockets.QWebSocket.maxAllowedIncomingMessageSize()¶
- Return type:
int
Returns the maximum allowed size of an incoming websocket message.
See also
- static PySide2.QtWebSockets.QWebSocket.maxIncomingFrameSize()¶
- Return type:
int
Returns the maximum supported size of an incoming websocket frame for this websocket implementation.
- static PySide2.QtWebSockets.QWebSocket.maxIncomingMessageSize()¶
- Return type:
int
Returns the maximum supported size of an incoming websocket message for this websocket implementation.
- static PySide2.QtWebSockets.QWebSocket.maxOutgoingFrameSize()¶
- Return type:
int
Returns the maximum supported size of an outgoing websocket frame for this websocket implementation.
- PySide2.QtWebSockets.QWebSocket.open(request)¶
- Parameters:
request –
PySide2.QtNetwork.QNetworkRequest
- PySide2.QtWebSockets.QWebSocket.open(url)
- Parameters:
url –
PySide2.QtCore.QUrl
- PySide2.QtWebSockets.QWebSocket.origin()¶
- Return type:
str
Returns the current origin.
- PySide2.QtWebSockets.QWebSocket.outgoingFrameSize()¶
- Return type:
int
Returns the maximum size of an outgoing websocket frame.
See also
- PySide2.QtWebSockets.QWebSocket.pauseMode()¶
- Return type:
PauseModes
Returns the pause mode of this socket
See also
- PySide2.QtWebSockets.QWebSocket.peerAddress()¶
- Return type:
Returns the peer address
- PySide2.QtWebSockets.QWebSocket.peerName()¶
- Return type:
str
Returns the
- PySide2.QtWebSockets.QWebSocket.peerPort()¶
- Return type:
quint16
Returns the peerport
- PySide2.QtWebSockets.QWebSocket.ping([payload=QByteArray()])¶
- Parameters:
payload –
PySide2.QtCore.QByteArray
Pings the server to indicate that the connection is still alive. Additional
payload
can be sent along the ping message.The size of the
payload
cannot be bigger than 125. If it is larger, thepayload
is clipped to 125 bytes.Note
QWebSocket
andQWebSocketServer
handles ping requests internally, which means they automatically send back a pong response to the peer.See also
- PySide2.QtWebSockets.QWebSocket.pong(elapsedTime, payload)¶
- Parameters:
elapsedTime – int
payload –
PySide2.QtCore.QByteArray
- Parameters:
authenticator –
PySide2.QtNetwork.QSslPreSharedKeyAuthenticator
- PySide2.QtWebSockets.QWebSocket.proxy()¶
- Return type:
Returns the currently configured proxy
See also
- PySide2.QtWebSockets.QWebSocket.proxyAuthenticationRequired(proxy, pAuthenticator)¶
- Parameters:
proxy –
PySide2.QtNetwork.QNetworkProxy
pAuthenticator –
PySide2.QtNetwork.QAuthenticator
- PySide2.QtWebSockets.QWebSocket.readBufferSize()¶
- Return type:
int
Returns the size in bytes of the readbuffer that is used by the socket.
See also
- PySide2.QtWebSockets.QWebSocket.readChannelFinished()¶
- PySide2.QtWebSockets.QWebSocket.request()¶
- Return type:
Returns the request that was or will be used to open this socket.
- PySide2.QtWebSockets.QWebSocket.requestUrl()¶
- Return type:
Returns the url the socket is connected to or will connect to.
- PySide2.QtWebSockets.QWebSocket.resourceName()¶
- Return type:
str
Returns the name of the resource currently accessed.
- PySide2.QtWebSockets.QWebSocket.resume()¶
Continues data transfer on the socket. This method should only be used after the socket has been set to pause upon notifications and a notification has been received. The only notification currently supported is
sslErrors()
. Calling this method if the socket is not paused results in undefined behavior.See also
- PySide2.QtWebSockets.QWebSocket.sendBinaryMessage(data)¶
- Parameters:
data –
PySide2.QtCore.QByteArray
- Return type:
int
Sends the given
data
over the socket as a binary message and returns the number of bytes actually sent.See also
- PySide2.QtWebSockets.QWebSocket.sendTextMessage(message)¶
- Parameters:
message – str
- Return type:
int
Sends the given
message
over the socket as a text message and returns the number of bytes actually sent.See also
- PySide2.QtWebSockets.QWebSocket.setMaskGenerator(maskGenerator)¶
- Parameters:
maskGenerator –
PySide2.QtWebSockets.QMaskGenerator
Sets the generator to use for creating masks to
maskGenerator
. The defaultQWebSocket
generator can be reset by supplying a nullptr . The mask generator can be changed at any time, even while the connection is open.See also
- PySide2.QtWebSockets.QWebSocket.setMaxAllowedIncomingFrameSize(maxAllowedIncomingFrameSize)¶
- Parameters:
maxAllowedIncomingFrameSize – int
Sets the maximum allowed size of an incoming websocket frame to
maxAllowedIncomingFrameSize
. If an incoming frame exceeds this limit, the peer gets disconnected. The accepted range is between 0 andmaxIncomingFrameSize()
, default ismaxIncomingFrameSize()
. The purpose of this function is to avoid exhausting virtual memory.See also
- PySide2.QtWebSockets.QWebSocket.setMaxAllowedIncomingMessageSize(maxAllowedIncomingMessageSize)¶
- Parameters:
maxAllowedIncomingMessageSize – int
Sets the maximum allowed size of an incoming websocket message to
maxAllowedIncomingMessageSize
. If an incoming message exceeds this limit, the peer gets disconnected. The accepted range is between 0 andmaxIncomingMessageSize()
, default ismaxIncomingMessageSize()
. The purpose of this function is to avoid exhausting virtual memory.See also
- PySide2.QtWebSockets.QWebSocket.setOutgoingFrameSize(outgoingFrameSize)¶
- Parameters:
outgoingFrameSize – int
Sets the maximum size of an outgoing websocket frame to
outgoingFrameSize
. The accepted range is between 0 andmaxOutgoingFrameSize()
, default is 512kB. The purpose of this function is to adapt to the maximum allowed frame size of the receiver.See also
- PySide2.QtWebSockets.QWebSocket.setPauseMode(pauseMode)¶
- Parameters:
pauseMode –
PauseModes
Controls whether to pause upon receiving a notification. The
pauseMode
parameter specifies the conditions in which the socket should be paused.The only notification currently supported is
sslErrors()
. If set to PauseOnSslErrors, data transfer on the socket will be paused and needs to be enabled explicitly again by callingresume()
. By default, this option is set to PauseNever. This option must be called before connecting to the server, otherwise it will result in undefined behavior.See also
- PySide2.QtWebSockets.QWebSocket.setProxy(networkProxy)¶
- Parameters:
networkProxy –
PySide2.QtNetwork.QNetworkProxy
Sets the proxy to
networkProxy
See also
- PySide2.QtWebSockets.QWebSocket.setReadBufferSize(size)¶
- Parameters:
size – int
Sets the size of
QWebSocket
‘s internal read buffer to besize
bytes.If the buffer size is limited to a certain size,
QWebSocket
won’t buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default. This option is useful if you only read the data at certain points in time (for example, in a real-time streaming application) or if you want to protect your socket against receiving too much data, which may eventually cause your application to run out of memory.See also
- PySide2.QtWebSockets.QWebSocket.state()¶
- Return type:
Returns the current state of the socket.
- PySide2.QtWebSockets.QWebSocket.stateChanged(state)¶
- Parameters:
state –
SocketState
- PySide2.QtWebSockets.QWebSocket.textFrameReceived(frame, isLastFrame)¶
- Parameters:
frame – str
isLastFrame – bool
- PySide2.QtWebSockets.QWebSocket.textMessageReceived(message)¶
- Parameters:
message – str
© 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.