Skip to content Skip to sidebar Skip to footer

How To Create A Websocket Client By Using Qwebsocket In Pyqt5

I want create a websocket client by using QWebSocket in PyQt5.For convenience, assume I have a websocket server, source code is like this , from PyQt5 import QtCore, QtWebSockets,

Solution 1:

This is a typical problem with qt console programs, you need to call your client methods outside of the python constructor (__init__).

I modified your server a little bit, adding some error tests (nothing really new):

from PyQt5 import QtCore, QtWebSockets, QtNetwork, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QAction
from PyQt5.QtCore import QUrl

classMyServer(QtCore.QObject):
    def__init__(self, parent):
        super(QtCore.QObject, self).__init__(parent)
        self.clients = []
        print("server name: {}".format(parent.serverName()))
        self.server = QtWebSockets.QWebSocketServer(parent.serverName(), parent.secureMode(), parent)
        if self.server.listen(QtNetwork.QHostAddress.LocalHost, 1302):
            print('Listening: {}:{}:{}'.format(
                self.server.serverName(), self.server.serverAddress().toString(),
                str(self.server.serverPort())))
        else:
            print('error')
        self.server.acceptError.connect(self.onAcceptError)
        self.server.newConnection.connect(self.onNewConnection)
        self.clientConnection = Noneprint(self.server.isListening())

    defonAcceptError(accept_error):
        print("Accept Error: {}".format(accept_error))

    defonNewConnection(self):
        print("onNewConnection")
        self.clientConnection = self.server.nextPendingConnection()
        self.clientConnection.textMessageReceived.connect(self.processTextMessage)

        self.clientConnection.textFrameReceived.connect(self.processTextFrame)

        self.clientConnection.binaryMessageReceived.connect(self.processBinaryMessage)
        self.clientConnection.disconnected.connect(self.socketDisconnected)

        print("newClient")
        self.clients.append(self.clientConnection)

    defprocessTextFrame(self, frame, is_last_frame):
        print("in processTextFrame")
        print("\tFrame: {} ; is_last_frame: {}".format(frame, is_last_frame))

    defprocessTextMessage(self, message):
        print("processTextMessage - message: {}".format(message))
        if self.clientConnection:
            for client in self.clients:
                # if client!= self.clientConnection:
                client.sendTextMessage(message)
            # self.clientConnection.sendTextMessage(message)defprocessBinaryMessage(self, message):
        print("b:",message)
        if self.clientConnection:
            self.clientConnection.sendBinaryMessage(message)

    defsocketDisconnected(self):
        print("socketDisconnected")
        if self.clientConnection:
            self.clients.remove(self.clientConnection)
            self.clientConnection.deleteLater()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    serverObject = QtWebSockets.QWebSocketServer('My Socket', QtWebSockets.QWebSocketServer.NonSecureMode)
    server = MyServer(serverObject)
    serverObject.closed.connect(app.quit)
    app.exec_()

The client uses some QTimer to call the required methods outside of the __init__method. I also added ping / pong methods to check the connection :

import sys

from PyQt5 import QtCore, QtWebSockets, QtNetwork
from PyQt5.QtCore import QUrl, QCoreApplication, QTimer
from PyQt5.QtWidgets import QApplication


classClient(QtCore.QObject):
    def__init__(self, parent):
        super().__init__(parent)

        self.client =  QtWebSockets.QWebSocket("",QtWebSockets.QWebSocketProtocol.Version13,None)
        self.client.error.connect(self.error)

        self.client.open(QUrl("ws://127.0.0.1:1302"))
        self.client.pong.connect(self.onPong)

    defdo_ping(self):
        print("client: do_ping")
        self.client.ping(b"foo")

    defsend_message(self):
        print("client: send_message")
        self.client.sendTextMessage("asd")

    defonPong(self, elapsedTime, payload):
        print("onPong - time: {} ; payload: {}".format(elapsedTime, payload))

    deferror(self, error_code):
        print("error code: {}".format(error_code))
        print(self.client.errorString())

    defclose(self):
        self.client.close()

defquit_app():
    print("timer timeout - exiting")
    QCoreApplication.quit()

defping():
    client.do_ping()

defsend_message():
    client.send_message()

if __name__ == '__main__':
    global client
    app = QApplication(sys.argv)

    QTimer.singleShot(2000, ping)
    QTimer.singleShot(3000, send_message)
    QTimer.singleShot(5000, quit_app)

    client = Client(app)

    app.exec_()

Server output:

G:\Qt\QtTests>pythonso_qwebsocket_server.pyserver name:MySocketListening:MySocket:127.0.0.1:1302TrueonNewConnectionnewClientinprocessTextFrameFrame:asd;is_last_frame:TrueprocessTextMessage - message:asdsocketDisconnected

client output:

G:\Qt\QtTests>python so_qwebsocket_client.py
client: do_ping
onPong - time: 0 ; payload: b'foo'client: send_message
timer timeout

All in all, if you use your client in a simple GUI (e.g. moving the client.sendTextMessage() outside __init__ and connecting a button click to actually send the message), due to its asynchronous nature, it should work without any problems!

Post a Comment for "How To Create A Websocket Client By Using Qwebsocket In Pyqt5"