[PyQt] QWebChannel official example

Filippo Cucchetto filippocucchetto at gmail.com
Mon Dec 26 13:39:20 GMT 2016


Is QWebChannel supported by PyQt5?
Beucase i tried to port the chat official example of Qt and blocked when
implementing the WebSocketTransport class.
See
https://code.qt.io/cgit/qt/qtwebchannel.git/tree/examples/webchannel/shared/websockettransport.cpp

The following code print this error when trying to emit the messageReceived
signal

TypeError: messageReceived(self, Dict[str, Union[QJsonValue,
QJsonValue.Type, Iterable[QJsonValue], bool, int, float, str]],
QWebChannelAbstractTransport).emit(): argument 1 has unexpected type 'dict'

class WebSocketTransport(QWebChannelAbstractTransport):

    def __init__(self, logger, server_socket, client_socket, *args, **kwargs):
        super(WebSocketTransport, self).__init__(*args, **kwargs)
        self.logger = logger
        self.server_socket = server_socket
        self.client_socket = client_socket
        self.client_socket.textMessageReceived.connect(self.on_text_message_received)
        self.client_socket.disconnected.connect(self.on_disconnected)

    def sendMessage(self, message, *args, **kwargs):
        self.logger.debug(message)
        document = QJsonDocument()
        document.setObject(message)
        document_as_bytes = document.toJson(QJsonDocument.Compact)
        document_as_str = document_as_bytes.decode('utf-8')
        self.client_socket.sendTextMessage(document_as_str)

    def on_text_message_received(self, message):
        self.logger.debug('Message:' + message)
        document = QJsonDocument()
        error = QJsonParseError()
        document = document.fromJson(message.encode('utf-8'), error)
        if error.error:
            self.logger.debug('Failed to parse ' + message + 'as Json object')
            self.logger.debug('Error is ' + error.errorString())
            return
        if not document.isObject():
            self.logger.debug('Message is not an object')
            return
        self.messageReceived.emit(document.object(), self)

    def on_disconnected(self):
        self.server_socket.on_client_disconnected(self)



Is it possible to have that official example implemented in the pyqt5
examples?

-- 
Filippo Cucchetto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20161226/9c74ee2c/attachment.html>


More information about the PyQt mailing list