The problem:I want to make client to retry connection if server is unreachable by sending ping/text message using qtimer.Client is expecting to get pong/text message from server to start processing some data.
Server is started without listening for incoming connection for first 20 seconds to imitate connection issue, and after 20 seconds i set server to listen so it can accept incoming connections.
If client can't connect to server in first attempt or during the 20 seconds period of not listening it will not connect even if server will start listening later, however if i run client when server switched to listening state everything will work. How i can i fix it so client will actually send ping/text message, because right now it looks like client is entering a loop, which prevents it from sending ping/text message.
Client part:
from time import sleepimport sysfrom PySide6 import QtCore, QtWebSocketsfrom PySide6.QtCore import QUrl, QTimerfrom PySide6.QtWidgets import QApplicationclass Client(QtCore.QObject): def __init__(self, parent): super().__init__(parent) self.client = QtWebSockets.QWebSocket("", QtWebSockets.QWebSocketProtocol.Version13, None) self.client.open(QUrl("ws://127.0.0.1:6000")) self.client.textMessageReceived.connect(self.process_message) self.client.pong.connect(self.get_pong) # self.client.error.connect(self.error) self.date_list = ['2000-01-01'] self.check_status_timer = QTimer() self.check_status_timer.timeout.connect(self.send_ping) self.check_status_timer.start(2000) # some data processing after connection established def process(self, day): print(f"Day {day}") for i in range(10): print(f"Sleeping {9 - i}") sleep(.5) print("emitted") return True def create_and_run(self): current_date = self.date_list[0] result = self.process(current_date) if result: self.send_message() else: print("PROCESS FAILED") def process_message(self, message): if message == "ping" or message == "True": self.check_status_timer.stop() self.create_and_run() def get_pong(self): print("PONG") def send_ping(self): print("SENDING PING") self.client.ping() self.client.sendTextMessage("ping") def send_message(self): self.client.sendTextMessage("Client0") def error(self, error_code): print("error code: {}".format(error_code))if __name__ == "__main__": app = QApplication(sys.argv) client = Client(app) app.exec()
Server part
from PySide6 import QtCore, QtWebSockets, QtNetworkfrom PySide6.QtWidgets import QApplicationclass MyServer(QtCore.QObject): def __init__(self, parent): super().__init__(parent) self.clients = [] self.server = QtWebSockets.QWebSocketServer(parent.serverName(), parent.secureMode(), parent) self.server.acceptError.connect(self.on_accept_error) self.server.newConnection.connect(self.on_new_connection) # create timer so server will switch to listening state later self.count = 0 self.timer_listen = QtCore.QTimer() self.timer_listen.timeout.connect(self.make_listen) self.timer_listen.start(2000) def make_listen(self): if self.count >= 10: self.timer_listen.stop() self.server.listen(QtNetwork.QHostAddress.LocalHost, 6000) print(self.count, self.server.isListening()) else: print(self.count, self.server.isListening()) self.count += 1 def on_accept_error(accept_error): print("Accept Error: {}".format(accept_error)) def on_new_connection(self): client_connection = self.server.nextPendingConnection() client_connection.textMessageReceived.connect(self.process_message) client_connection.disconnected.connect(self.disconnect) self.clients.append(client_connection) def process_message(self, message): print(message) if message == "ping": self.sender().sendTextMessage("ping") def disconnect(self): if self.sender() in self.clients: self.clients.remove(self.sender()) self.sender().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()
I tried to utilize worker class to prevent client's main loop to freeze, and i tried to subclass QWebsocket, but had no luck.