websocket fix
This commit is contained in:
parent
731fd56768
commit
4e08d81bb0
@ -16,6 +16,8 @@
|
||||
"path": "data/backups",
|
||||
"keep_days": 30
|
||||
},
|
||||
"other": {},
|
||||
"other": {
|
||||
"anime_directory": "/home/lukas/Volume/serien/"
|
||||
},
|
||||
"version": "1.0.0"
|
||||
}
|
||||
@ -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"
|
||||
}
|
||||
@ -68,6 +68,9 @@ conda run -n AniWorld python -m pytest tests/ -v -k "auth"
|
||||
|
||||
# Show all print statements
|
||||
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
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@ -21,7 +21,6 @@ from src.server.services.websocket_service import (
|
||||
WebSocketService,
|
||||
get_websocket_service,
|
||||
)
|
||||
from src.server.utils.dependencies import get_current_user_optional
|
||||
|
||||
logger = structlog.get_logger(__name__)
|
||||
|
||||
@ -31,8 +30,8 @@ router = APIRouter(prefix="/ws", tags=["websocket"])
|
||||
@router.websocket("/connect")
|
||||
async def websocket_endpoint(
|
||||
websocket: WebSocket,
|
||||
token: Optional[str] = None,
|
||||
ws_service: WebSocketService = Depends(get_websocket_service),
|
||||
user_id: Optional[str] = Depends(get_current_user_optional),
|
||||
):
|
||||
"""WebSocket endpoint for client connections.
|
||||
|
||||
@ -40,6 +39,10 @@ async def websocket_endpoint(
|
||||
The connection is maintained until the client disconnects or
|
||||
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:
|
||||
1. Client connects
|
||||
2. Server sends "connected" message
|
||||
@ -70,6 +73,20 @@ async def websocket_endpoint(
|
||||
```
|
||||
"""
|
||||
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:
|
||||
# Accept connection and register with service
|
||||
|
||||
@ -161,11 +161,10 @@ class WebSocketClient {
|
||||
/**
|
||||
* Send message to server
|
||||
*/
|
||||
send(type, data = {}) {
|
||||
send(action, data = {}) {
|
||||
const message = JSON.stringify({
|
||||
type,
|
||||
data,
|
||||
timestamp: new Date().toISOString()
|
||||
action,
|
||||
data
|
||||
});
|
||||
|
||||
if (this.isConnected && this.ws.readyState === WebSocket.OPEN) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user