-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
50 lines (40 loc) · 1.66 KB
/
player.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
import select
import socket
import sys
import threading
from typing import override
from client import Client
class Player(Client):
def __init__(self, host, port, close_event):
super().__init__(host, port, close_event)
@override
def receive_messages(self):
while not self.close_event.is_set():
sockets_list = [sys.stdin, self.server_socket]
try:
read_sockets, write_socket, error_socket = select.select(
sockets_list, [], [])
for socks in read_sockets:
if socks == self.server_socket:
message = socks.recv(2048)
decoded_message = message.decode()
if (decoded_message == "close"):
self._close_connection_from_server()
if decoded_message.endswith("quit the game. You win :)"):
self.messages_logger.info(message.decode().strip())
self.server_socket.send("quit".encode())
else:
self.messages_logger.info(message.decode().strip())
except socket.error as e:
self.logger.error(f"Error connecting to the server: {e}")
if __name__ == "__main__":
close_event = threading.Event()
client = Player("127.0.0.1", 12345, close_event)
client.connect()
send_thread = threading.Thread(
target=client.send_message)
receive_thread = threading.Thread(target=client.receive_messages)
send_thread.start()
receive_thread.start()
close_event.wait()
print("Press <Enter> to exit...")