Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #28

Merged
merged 2 commits into from
Mar 7, 2024
Merged

Dev #28

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion echo-client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!pip install rich
#!pip install prompt_toolkit
from rich.console import Console
from prompt_toolkit import PromptSession
from prompt_toolkit.styles import Style
Expand All @@ -7,7 +9,7 @@
import sys

HOST = "127.0.0.1"
PORT = 65436
PORT = 65433

console = Console()
style = Style.from_dict({
Expand Down
30 changes: 16 additions & 14 deletions echo-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys

HOST = "127.0.0.1"
PORT = 65436
PORT = 65433
TIEMPO_ESPERA = 100 # segundos

# Diccionario para almacenar los canales y usuarios
Expand Down Expand Up @@ -71,7 +71,7 @@ def register_user(data, addr, conn):
username = parts[1]
if username not in users: # Solo registra si el usuario es nuevo
with lock:
users[username] = addr[0]
users[username] = {"ip": addr[0], "conn": conn}
response_to_client = f"Bienvenido, {username}! Tu dirección IP es {addr[0]}\n"
conn.sendall(response_to_client.encode())
print(f"Usuario registrado: {username} - {addr[0]}")
Expand Down Expand Up @@ -117,14 +117,14 @@ def join_channel(conn, input_client, username, addr):
conn.sendall("Formato incorrecto. Usa /JOIN [nombreDelCanal]".encode('utf-8'))
return
channel_name = parts[1].strip()
if channel_name in channels:
if username not in channels[channel_name]:
channels[channel_name][username] = {"ip": addr}
conn.sendall(f"Te has unido al canal '{channel_name}'.".encode('utf-8'))
else:
conn.sendall(f"Ya estás en el canal '{channel_name}'.".encode('utf-8'))
if channel_name not in channels:
channels[channel_name] = {}
# Asegúrate de que el usuario está registrado antes de añadirlo a un canal.
if username in users:
channels[channel_name][username] = users[username]
conn.sendall(f"Te has unido al canal '{channel_name}'.".encode('utf-8'))
else:
conn.sendall(f"El canal '{channel_name}' no existe.".encode('utf-8'))
conn.sendall("Primero debes registrarte.".encode('utf-8'))



Expand All @@ -135,11 +135,13 @@ def send_message(conn, input_client, username):
conn.sendall("Formato incorrecto. Usa /MSG [canal] [mensaje]".encode('utf-8'))
return
channel, message_to_send = parts[1], parts[2]
if channel in channels and username in channels[channel]:
response_to_client = (f"{username} (en {channel}): {message_to_send}")
conn.sendall(response_to_client.encode('utf-8'))
else:
conn.sendall("No estás en ese canal o el canal no existe.".encode('utf-8'))
if channel in channels:
for user, user_info in channels[channel].items():
user_conn = user_info["conn"]
try:
user_conn.sendall(f"{username} (en {channel}): {message_to_send}".encode('utf-8'))
except Exception as e:
print(f"Error al enviar mensaje a {user}: {e}")


def send_whisper(conn, input_client, username):
Expand Down