fixed bugs
This commit is contained in:
45
client.py
45
client.py
@@ -55,6 +55,20 @@ def term_size():
|
|||||||
def print_white(text=""):
|
def print_white(text=""):
|
||||||
print(wit + text + reset)
|
print(wit + text + reset)
|
||||||
|
|
||||||
|
def recv_json(sock):
|
||||||
|
"""Veilig JSON ontvangen, ook bij grote berichten."""
|
||||||
|
buffer = b""
|
||||||
|
while True:
|
||||||
|
part = sock.recv(4096)
|
||||||
|
if not part:
|
||||||
|
break
|
||||||
|
buffer += part
|
||||||
|
try:
|
||||||
|
return json.loads(buffer.decode("utf-8"))
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue
|
||||||
|
return {} # fallback, lege dict als niks lukt
|
||||||
|
|
||||||
def login():
|
def login():
|
||||||
base_dir = Path(__file__).parent
|
base_dir = Path(__file__).parent
|
||||||
passwd_file = base_dir / "passwd.json"
|
passwd_file = base_dir / "passwd.json"
|
||||||
@@ -98,8 +112,7 @@ while not current_user:
|
|||||||
w, _ = term_size()
|
w, _ = term_size()
|
||||||
print(blauw_bg + wit + "Terminal Chat ".center(w) + reset)
|
print(blauw_bg + wit + "Terminal Chat ".center(w) + reset)
|
||||||
print()
|
print()
|
||||||
|
print(groen + "========================================")
|
||||||
print( groen + "========================================")
|
|
||||||
print(" Welcome to the Terminal Chat")
|
print(" Welcome to the Terminal Chat")
|
||||||
print("========================================" + reset)
|
print("========================================" + reset)
|
||||||
print()
|
print()
|
||||||
@@ -123,8 +136,7 @@ except Exception as e:
|
|||||||
print("Connection failed:", e)
|
print("Connection failed:", e)
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
data = s.recv(4096)
|
chat_data = recv_json(s)
|
||||||
chat_data = json.loads(data.decode("utf-8"))
|
|
||||||
chat_names = list(chat_data.keys())
|
chat_names = list(chat_data.keys())
|
||||||
|
|
||||||
def draw_chat_window(messages, chatname):
|
def draw_chat_window(messages, chatname):
|
||||||
@@ -143,42 +155,32 @@ def draw_chat_window(messages, chatname):
|
|||||||
print("-" * w)
|
print("-" * w)
|
||||||
|
|
||||||
def chat_session(chatkeuze):
|
def chat_session(chatkeuze):
|
||||||
global chat_messages
|
global chat_messages, chat_users
|
||||||
global chat_users
|
|
||||||
|
|
||||||
s.sendall(json.dumps({"action": "openchat", "chat_name": chatkeuze}).encode("utf-8"))
|
s.sendall(json.dumps({"action": "openchat", "chat_name": chatkeuze}).encode("utf-8"))
|
||||||
data = s.recv(4096)
|
chat_data = recv_json(s)
|
||||||
chat_data = json.loads(data.decode("utf-8"))
|
|
||||||
|
|
||||||
if chatkeuze in chat_data:
|
if chatkeuze in chat_data:
|
||||||
chat_messages = chat_data[chatkeuze].get("messages", [])
|
chat_messages = chat_data[chatkeuze].get("messages", [])
|
||||||
chat_users = chat_data[chatkeuze].get("users", [])
|
chat_users = chat_data[chatkeuze].get("users", [])
|
||||||
|
|
||||||
def receive_messages(sock):
|
def receive_messages(sock):
|
||||||
global chat_messages
|
global chat_messages, chat_users
|
||||||
global chat_users
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
data = sock.recv(4096)
|
decoded = recv_json(sock)
|
||||||
if not data:
|
if not decoded:
|
||||||
break
|
break
|
||||||
|
|
||||||
decoded = json.loads(data.decode("utf-8"))
|
|
||||||
|
|
||||||
# /users response
|
|
||||||
if isinstance(decoded, dict) and decoded.get("type") == "users_list":
|
if isinstance(decoded, dict) and decoded.get("type") == "users_list":
|
||||||
users = decoded.get("users", [])
|
users = decoded.get("users", [])
|
||||||
print("\nUsers:", ", ".join(users))
|
print("\nUsers:", ", ".join(users))
|
||||||
print(wit + "> " + reset, end="", flush=True)
|
print(wit + "> " + reset, end="", flush=True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# normale chat update
|
|
||||||
if isinstance(decoded, dict) and chatkeuze in decoded:
|
if isinstance(decoded, dict) and chatkeuze in decoded:
|
||||||
with lock:
|
with lock:
|
||||||
chat_messages = decoded[chatkeuze].get("messages", [])
|
chat_messages = decoded[chatkeuze].get("messages", [])
|
||||||
chat_users = decoded[chatkeuze].get("users", [])
|
chat_users = decoded[chatkeuze].get("users", [])
|
||||||
|
|
||||||
draw_chat_window(chat_messages, chatkeuze)
|
draw_chat_window(chat_messages, chatkeuze)
|
||||||
print(wit + "> " + reset, end="", flush=True)
|
print(wit + "> " + reset, end="", flush=True)
|
||||||
|
|
||||||
@@ -238,9 +240,8 @@ while True:
|
|||||||
if keuze.isdigit() and int(keuze) == len(chat_names)+1:
|
if keuze.isdigit() and int(keuze) == len(chat_names)+1:
|
||||||
nieuwe_chat = input("Name of new chat: ")
|
nieuwe_chat = input("Name of new chat: ")
|
||||||
s.sendall(json.dumps({"action": "new_chat", "chat_name": nieuwe_chat}).encode("utf-8"))
|
s.sendall(json.dumps({"action": "new_chat", "chat_name": nieuwe_chat}).encode("utf-8"))
|
||||||
data = s.recv(4096)
|
chat_data = recv_json(s)
|
||||||
chat_data = json.loads(data.decode("utf-8"))
|
|
||||||
chat_names = list(chat_data.keys())
|
chat_names = list(chat_data.keys())
|
||||||
elif keuze.isdigit() and 1 <= int(keuze) <= len(chat_names):
|
elif keuze.isdigit() and 1 <= int(keuze) <= len(chat_names):
|
||||||
chatkeuze = chat_names[int(keuze)-1]
|
chatkeuze = chat_names[int(keuze)-1]
|
||||||
chat_session(chatkeuze)
|
chat_session(chatkeuze)
|
||||||
Reference in New Issue
Block a user