feat(server): implement websocket chat backend
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
36
app/main.py
Normal file
36
app/main.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
|
from app.websocket.chat import router as chat_router
|
||||||
|
from app.websocket.manager import manager
|
||||||
|
|
||||||
|
BASE_DIR = Path(__file__).resolve().parent
|
||||||
|
STATIC_DIR = BASE_DIR / "static"
|
||||||
|
|
||||||
|
app = FastAPI(title="Simple WebSocket Chatroom")
|
||||||
|
|
||||||
|
# WebSocket route: ws://localhost:8000/ws/{room_id}/{client_id}
|
||||||
|
app.include_router(chat_router)
|
||||||
|
|
||||||
|
# Frontend files
|
||||||
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def home():
|
||||||
|
"""Serve the chatroom GUI."""
|
||||||
|
return FileResponse(STATIC_DIR / "index.html")
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/api/rooms")
|
||||||
|
async def rooms():
|
||||||
|
"""Small helper endpoint for the GUI/debugging."""
|
||||||
|
return manager.rooms_overview()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
async def health():
|
||||||
|
return {"status": "ok", "app": "Simple WebSocket Chatroom"}
|
||||||
0
app/websocket/__init__.py
Normal file
0
app/websocket/__init__.py
Normal file
87
app/websocket/chat.py
Normal file
87
app/websocket/chat.py
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import json
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
|
||||||
|
|
||||||
|
from app.websocket.manager import manager
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
def now_iso() -> str:
|
||||||
|
return datetime.now(timezone.utc).isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def event(event_type: str, room_id: str, client_id: str, content: str):
|
||||||
|
return {
|
||||||
|
"type": event_type,
|
||||||
|
"room_id": room_id,
|
||||||
|
"client_id": client_id,
|
||||||
|
"content": content,
|
||||||
|
"timestamp": now_iso(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def clean_value(value: str, fallback: str) -> str:
|
||||||
|
value = value.strip()
|
||||||
|
return value if value else fallback
|
||||||
|
|
||||||
|
|
||||||
|
@router.websocket("/ws/{room_id}/{client_id}")
|
||||||
|
async def websocket_endpoint(websocket: WebSocket, room_id: str, client_id: str):
|
||||||
|
room_id = clean_value(room_id, "عمومی")
|
||||||
|
client_id = clean_value(client_id, "ناشناس")
|
||||||
|
|
||||||
|
await manager.connect(websocket, room_id, client_id)
|
||||||
|
|
||||||
|
await manager.broadcast_json(
|
||||||
|
event("system", room_id, "سیستم", f"{client_id} وارد اتاق {room_id} شد."),
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
|
await manager.broadcast_json(
|
||||||
|
{
|
||||||
|
"type": "presence",
|
||||||
|
"room_id": room_id,
|
||||||
|
"client_id": "سیستم",
|
||||||
|
"content": "presence-updated",
|
||||||
|
"users": manager.room_users(room_id),
|
||||||
|
"timestamp": now_iso(),
|
||||||
|
},
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
raw_data = await websocket.receive_text()
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = json.loads(raw_data)
|
||||||
|
message = str(data.get("content", "")).strip()
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
message = raw_data.strip()
|
||||||
|
|
||||||
|
if not message:
|
||||||
|
continue
|
||||||
|
|
||||||
|
await manager.broadcast_json(
|
||||||
|
event("message", room_id, client_id, message),
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
except WebSocketDisconnect:
|
||||||
|
manager.disconnect(websocket, room_id)
|
||||||
|
await manager.broadcast_json(
|
||||||
|
event("system", room_id, "سیستم", f"{client_id} از اتاق {room_id} خارج شد."),
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
|
await manager.broadcast_json(
|
||||||
|
{
|
||||||
|
"type": "presence",
|
||||||
|
"room_id": room_id,
|
||||||
|
"client_id": "سیستم",
|
||||||
|
"content": "presence-updated",
|
||||||
|
"users": manager.room_users(room_id),
|
||||||
|
"timestamp": now_iso(),
|
||||||
|
},
|
||||||
|
room_id,
|
||||||
|
)
|
||||||
74
app/websocket/manager.py
Normal file
74
app/websocket/manager.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from fastapi import WebSocket
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ClientConnection:
|
||||||
|
websocket: WebSocket
|
||||||
|
client_id: str
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectionManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.active_connections: dict[str, list[ClientConnection]] = {}
|
||||||
|
|
||||||
|
async def connect(self, websocket: WebSocket, room_id: str, client_id: str):
|
||||||
|
await websocket.accept()
|
||||||
|
self.active_connections.setdefault(room_id, [])
|
||||||
|
self.active_connections[room_id].append(
|
||||||
|
ClientConnection(websocket=websocket, client_id=client_id)
|
||||||
|
)
|
||||||
|
|
||||||
|
def disconnect(self, websocket: WebSocket, room_id: str):
|
||||||
|
if room_id not in self.active_connections:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.active_connections[room_id] = [
|
||||||
|
connection
|
||||||
|
for connection in self.active_connections[room_id]
|
||||||
|
if connection.websocket is not websocket
|
||||||
|
]
|
||||||
|
|
||||||
|
if not self.active_connections[room_id]:
|
||||||
|
del self.active_connections[room_id]
|
||||||
|
|
||||||
|
async def send_json(self, websocket: WebSocket, payload: dict[str, Any]):
|
||||||
|
await websocket.send_json(payload)
|
||||||
|
|
||||||
|
async def broadcast_json(self, payload: dict[str, Any], room_id: str):
|
||||||
|
if room_id not in self.active_connections:
|
||||||
|
return
|
||||||
|
|
||||||
|
dead_connections: list[WebSocket] = []
|
||||||
|
|
||||||
|
for connection in list(self.active_connections[room_id]):
|
||||||
|
try:
|
||||||
|
await connection.websocket.send_json(payload)
|
||||||
|
except Exception:
|
||||||
|
dead_connections.append(connection.websocket)
|
||||||
|
|
||||||
|
for websocket in dead_connections:
|
||||||
|
self.disconnect(websocket, room_id)
|
||||||
|
|
||||||
|
def room_users(self, room_id: str) -> list[str]:
|
||||||
|
return [
|
||||||
|
connection.client_id
|
||||||
|
for connection in self.active_connections.get(room_id, [])
|
||||||
|
]
|
||||||
|
|
||||||
|
def rooms_overview(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"rooms": [
|
||||||
|
{
|
||||||
|
"room_id": room_id,
|
||||||
|
"online_count": len(connections),
|
||||||
|
"users": [connection.client_id for connection in connections],
|
||||||
|
}
|
||||||
|
for room_id, connections in self.active_connections.items()
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
manager = ConnectionManager()
|
||||||
Reference in New Issue
Block a user