-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
62 lines (44 loc) · 1.58 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Source: https://stackoverflow.com/questions/35237245/how-to-create-a-websocket-client-by-using-qwebsocket-in-pyqt5
import sys
from PyQt6 import QtCore, QtWebSockets, QtNetwork
from PyQt6.QtCore import QUrl, QCoreApplication, QTimer
from PyQt6.QtWidgets import QApplication
import json
class Client(QtCore.QObject):
def __init__(self, parent):
super().__init__(parent)
self.client = QtWebSockets.QWebSocket("",QtWebSockets.QWebSocketProtocol.Version.Version13,None)
self.client.error.connect(self.error)
self.client.open(QUrl("ws://127.0.0.1:1302"))
self.client.pong.connect(self.onPong)
def do_ping(self):
print("client: do_ping")
self.client.ping(b"foo")
def send_message(self):
print("client: send_message")
self.client.sendTextMessage(json.dumps({'user': "lol", 'msg': "ping"}))
def onPong(self, elapsedTime, payload):
print("onPong - time: {} ; payload: {}".format(elapsedTime, payload))
def error(self, error_code):
print("error code: {}".format(error_code))
print(self.client.errorString())
def close(self):
self.client.close()
def quit_app():
print("timer timeout - exiting")
QCoreApplication.quit()
def ping():
client.do_ping()
def send_message():
client.send_message()
def do_spam():
while True:
ping()
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()