-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocketHandler.py
40 lines (34 loc) · 1.32 KB
/
socketHandler.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
import socket
import threading
class SocketHandler:
def __init__(self, host, port, receive_callback=None):
self.host = host
self.port = port
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.receive_event = threading.Event()
self.receive_callback = receive_callback
def connect(self):
self.client_socket.connect((self.host, self.port))
print(f"Connected to {self.host}:{self.port}")
def send_message(self, message):
self.client_socket.sendall(message.encode('utf-8'))
def receive_messages(self):
while True:
try:
self.client_socket.settimeout(5)
data = self.client_socket.recv(20000)
if data:
message = data.decode('utf-8')
#print(f"Received message: {message}")
if self.receive_callback:
self.receive_callback(message)
self.receive_event.set()
except socket.error:
# No data received
pass
def start_receive_thread(self):
receive_thread = threading.Thread(target=self.receive_messages)
receive_thread.daemon = True
receive_thread.start()
def close(self):
self.client_socket.close()