feat(server): implement websocket chat backend
This commit is contained in:
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,
|
||||
)
|
||||
Reference in New Issue
Block a user