mirror of
https://github.com/tcsenpai/telnet_retro_chat.git
synced 2025-06-06 19:25:35 +00:00
27 lines
917 B
Python
27 lines
917 B
Python
def broadcast_message(
|
|
connections, message, sender_addr=None, room=None, system_msg=False
|
|
):
|
|
"""Broadcasts a message to all users in a room or system-wide."""
|
|
formatted_message = f"\r\n{message}\r\n"
|
|
encoded_message = formatted_message.encode("ascii")
|
|
|
|
# Get recipients based on room, unless it's a system message
|
|
recipients = (
|
|
connections.keys()
|
|
if system_msg
|
|
else (room.users if room else connections.keys())
|
|
)
|
|
|
|
for addr in recipients:
|
|
if addr not in connections:
|
|
continue
|
|
try:
|
|
if sender_addr and addr == sender_addr:
|
|
continue
|
|
connections[addr].sendall(encoded_message)
|
|
except (ConnectionError, BrokenPipeError):
|
|
print(f"Error sending message to {addr}")
|
|
except:
|
|
# If sending fails, we'll let the main loop handle the disconnection
|
|
pass
|