You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/playground/demos/swarm_hackathon/Bants.py

39 lines
1.2 KiB

# Import the necessary libraries.
import asyncio
import websockets
# Create a list of public group chats.
public_group_chats = []
# Create a function to handle incoming websocket connections.
async def handle_websocket(websocket, path):
# Get the username of the user.
username = await websocket.recv()
print(f"New connection from {username}")
# Add the user to the list of public group chats.
public_group_chats.append(websocket)
try:
# Wait for the user to send a message.
while True:
message = await websocket.recv()
print(f"{username}: {message}")
# Broadcast the message to all other users in the public group chats.
for other_websocket in public_group_chats:
if other_websocket != websocket:
9 months ago
await other_websocket.send(f"{username}: {message}")
finally:
# Remove the user from the list of public group chats.
public_group_chats.remove(websocket)
print(f"{username} has disconnected")
# Create a websocket server.
server = websockets.serve(handle_websocket, "localhost", 8000)
# Run the websocket server.
asyncio.run(server)