diff --git a/server.py b/server.py
index 9683b28..b09bb6c 100755
--- a/server.py
+++ b/server.py
@@ -12,6 +12,7 @@
import asyncio
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
+from fastapi.responses import HTMLResponse
import json
from redis import asyncio as redis
@@ -24,6 +25,43 @@ templates = Jinja2Templates(directory="templates")
# Redis connection
redis_client = redis.Redis(host="localhost", port=6379, decode_responses=True)
+new_button = """
+
+
+
+
+
+"""
# Constants
REDIS_MESSAGE_LIST = "chat_messages"
WEBSOCKET_CHANNEL = "websocket_channel"
@@ -41,6 +79,7 @@ async def listen_to_redis():
await pubsub.subscribe(WEBSOCKET_CHANNEL)
async for message in pubsub.listen():
+ print(message)
if message["type"] == "message":
data = message["data"]
data = json.loads(data)
@@ -52,11 +91,16 @@ async def listen_to_redis():
color_level = color_levels[level]
# Send message to all connected clients
for name, websocket in connected_clients.items():
- if data.get("name") == name:
- html_data = f'{data.get("name")}: {data.get("chat_message")}'
- else:
- html_data = f'{data.get("name")}: {data.get("chat_message")}'
- await websocket.send_text(html_data)
+ html_data = ""
+
+ if "chat_message" in data:
+ if data.get("name") == name:
+ html_data += f'{data.get("name")}: {data.get("chat_message")}'
+ else:
+ html_data += f'{data.get("name")}: {data.get("chat_message")}'
+ if "notification" in data:
+ html_data += f'{data.get("notification")}'
+ await websocket.send_text(html_data + new_button)
@app.on_event("startup")
@@ -106,28 +150,41 @@ async def websocket_endpoint(websocket: WebSocket):
name = random.choice(chat_names)
connected_clients[name] = websocket
-
+ data = {
+ "name": name,
+ "notification": f"{name} joined the chat",
+ }
+
+ data_str = json.dumps(data)
+
+ await redis_client.publish(WEBSOCKET_CHANNEL, data_str)
+
# Send previous messages to the new client
messages = await redis_client.lrange(REDIS_MESSAGE_LIST, 0, -1)
for msg in messages:
msg_data = json.loads(msg)
color_level = color_levels[0] # Use first color level for old messages
- if msg_data.get("name") == name:
- html_data = f'{msg_data.get("name")}: {msg_data.get("chat_message")}'
- else:
- html_data = f'{msg_data.get("name")}: {msg_data.get("chat_message")}'
- await websocket.send_text(html_data)
-
+ html_data = ""
+ if "chat_message" in msg_data:
+ if msg_data.get("name") == name:
+ html_data += f'{msg_data.get("name")}: {msg_data.get("chat_message")}'
+
+ else:
+ html_data += f'{msg_data.get("name")}: {msg_data.get("chat_message")}'
+ if "notification" in msg_data:
+ html_data += f'{msg_data.get("notification")}'
+ await websocket.send_text(html_data + new_button)
+
try:
while True:
data = await websocket.receive_text()
data = json.loads(data)
data["name"] = name
data_str = json.dumps(data)
-
+
# Store message in Redis list
await redis_client.rpush(REDIS_MESSAGE_LIST, data_str)
-
+
# Publish message to Redis
await redis_client.publish(WEBSOCKET_CHANNEL, data_str)
except WebSocketDisconnect:
@@ -139,6 +196,11 @@ async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
+@app.get("/null")
+async def root(request: Request):
+ return HTMLResponse("")
+
+
if __name__ == "__main__":
import uvicorn
diff --git a/templates/index.html b/templates/index.html
index 5c783ab..813a65b 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -7,62 +7,78 @@
-
+
-
+
+
WebSocket HTML Example
-
Disconnected
-
-
+
+
+
+