websocket fix

This commit is contained in:
Lukas 2025-10-24 20:10:40 +02:00
parent 731fd56768
commit 4e08d81bb0
5 changed files with 28 additions and 28 deletions

View File

@ -16,6 +16,8 @@
"path": "data/backups", "path": "data/backups",
"keep_days": 30 "keep_days": 30
}, },
"other": {}, "other": {
"anime_directory": "/home/lukas/Volume/serien/"
},
"version": "1.0.0" "version": "1.0.0"
} }

View File

@ -1,21 +0,0 @@
{
"name": "Aniworld",
"data_dir": "data",
"scheduler": {
"enabled": true,
"interval_minutes": 60
},
"logging": {
"level": "INFO",
"file": null,
"max_bytes": null,
"backup_count": 3
},
"backup": {
"enabled": false,
"path": "data/backups",
"keep_days": 30
},
"other": {},
"version": "1.0.0"
}

View File

@ -68,6 +68,9 @@ conda run -n AniWorld python -m pytest tests/ -v -k "auth"
# Show all print statements # Show all print statements
conda run -n AniWorld python -m pytest tests/ -v -s conda run -n AniWorld python -m pytest tests/ -v -s
#Run app
conda run -n AniWorld python -m uvicorn src.server.fastapi_app:app --host 127.0.0.1 --port 8000 --reload
``` ```
--- ---

View File

@ -21,7 +21,6 @@ from src.server.services.websocket_service import (
WebSocketService, WebSocketService,
get_websocket_service, get_websocket_service,
) )
from src.server.utils.dependencies import get_current_user_optional
logger = structlog.get_logger(__name__) logger = structlog.get_logger(__name__)
@ -31,8 +30,8 @@ router = APIRouter(prefix="/ws", tags=["websocket"])
@router.websocket("/connect") @router.websocket("/connect")
async def websocket_endpoint( async def websocket_endpoint(
websocket: WebSocket, websocket: WebSocket,
token: Optional[str] = None,
ws_service: WebSocketService = Depends(get_websocket_service), ws_service: WebSocketService = Depends(get_websocket_service),
user_id: Optional[str] = Depends(get_current_user_optional),
): ):
"""WebSocket endpoint for client connections. """WebSocket endpoint for client connections.
@ -40,6 +39,10 @@ async def websocket_endpoint(
The connection is maintained until the client disconnects or The connection is maintained until the client disconnects or
an error occurs. an error occurs.
Authentication:
- Optional token can be passed as query parameter: /ws/connect?token=<jwt>
- Unauthenticated connections are allowed but may have limited access
Message flow: Message flow:
1. Client connects 1. Client connects
2. Server sends "connected" message 2. Server sends "connected" message
@ -70,6 +73,20 @@ async def websocket_endpoint(
``` ```
""" """
connection_id = str(uuid.uuid4()) connection_id = str(uuid.uuid4())
user_id: Optional[str] = None
# Optional: Validate token if provided
if token:
try:
from src.server.services.auth_service import auth_service
session = auth_service.create_session_model(token)
user_id = session.user_id
except Exception as e:
logger.warning(
"Invalid WebSocket authentication token",
connection_id=connection_id,
error=str(e),
)
try: try:
# Accept connection and register with service # Accept connection and register with service

View File

@ -161,11 +161,10 @@ class WebSocketClient {
/** /**
* Send message to server * Send message to server
*/ */
send(type, data = {}) { send(action, data = {}) {
const message = JSON.stringify({ const message = JSON.stringify({
type, action,
data, data
timestamp: new Date().toISOString()
}); });
if (this.isConnected && this.ws.readyState === WebSocket.OPEN) { if (this.isConnected && this.ws.readyState === WebSocket.OPEN) {