-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketClient.py
52 lines (36 loc) · 1.28 KB
/
SocketClient.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
from PyQt5 import QtCore, QtWebSockets
from PyQt5.QtCore import QUrl
class SocketClient(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)
def do_ping(self):
print("client: do_ping")
self.client.ping(b"foo")
def send_message(self, message):
print("client: send_message")
self.client.sendTextMessage(message)
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()'''
'''if __name__ == '__main__':
global client
app = QApplication(sys.argv)
QTimer.singleShot(2000, ping)
QTimer.singleShot(3000, send_message)
QTimer.singleShot(5000, quit_app)
app.exec_()'''