- Added MusicPlayerService for managing VLC music playback with playlist support. - Implemented methods for loading playlists, controlling playback (play, pause, stop, next, previous), and managing volume and play modes. - Integrated real-time synchronization with VLC state using a background thread. - Added SocketIO event emissions for player state updates. - Enhanced logging for better debugging and tracking of player state changes. fix: Improve SocketIO service logging and event handling - Added detailed logging for SocketIO events and user authentication. - Implemented a test event handler to verify SocketIO functionality. - Enhanced error handling and logging for better traceability. chore: Update dependencies and logging configuration - Added python-vlc dependency for VLC integration. - Configured logging to show INFO and DEBUG messages for better visibility during development. - Updated main application entry point to allow unsafe Werkzeug for debugging purposes.
197 lines
7.1 KiB
Python
197 lines
7.1 KiB
Python
"""SocketIO service for real-time communication."""
|
|
|
|
import logging
|
|
|
|
from flask import request
|
|
from flask_socketio import disconnect, emit, join_room, leave_room
|
|
|
|
from app import socketio
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Add debug print to ensure this module is loaded
|
|
print(f"🔧 SocketIO Service: Module loaded, logger level: {logger.level}")
|
|
print(f"🔧 SocketIO Service: Effective logger level: {logger.getEffectiveLevel()}")
|
|
print(f"🔧 SocketIO Service: Parent logger handlers: {logger.parent.handlers}")
|
|
print(f"🔧 SocketIO Service: Logger handlers: {logger.handlers}")
|
|
|
|
|
|
class SocketIOService:
|
|
"""Service for managing SocketIO connections and user rooms."""
|
|
|
|
@staticmethod
|
|
def get_user_room(user_id: int) -> str:
|
|
"""Get the room name for a specific user."""
|
|
return f"user_{user_id}"
|
|
|
|
@staticmethod
|
|
def emit_to_user(user_id: int, event: str, data: dict) -> None:
|
|
"""Emit an event to a specific user's room."""
|
|
room = SocketIOService.get_user_room(user_id)
|
|
socketio.emit(event, data, room=room)
|
|
logger.debug(f"Emitted {event} to user {user_id} in room {room}")
|
|
|
|
@staticmethod
|
|
def emit_to_all(event: str, data: dict) -> None:
|
|
"""Emit an event to all connected clients."""
|
|
try:
|
|
socketio.emit(event, data)
|
|
logger.info(f"Successfully emitted {event} to all clients with data keys: {list(data.keys())}")
|
|
except Exception as e:
|
|
logger.error(f"Failed to emit {event}: {e}")
|
|
|
|
@staticmethod
|
|
def emit_credits_changed(user_id: int, new_credits: int) -> None:
|
|
"""Emit credits_changed event to a user."""
|
|
SocketIOService.emit_to_user(
|
|
user_id,
|
|
"credits_changed",
|
|
{"credits": new_credits},
|
|
)
|
|
|
|
@staticmethod
|
|
def get_user_from_socketio() -> dict | None:
|
|
"""Get user from SocketIO connection using cookies."""
|
|
try:
|
|
from flask import current_app
|
|
from flask_jwt_extended import decode_token
|
|
|
|
# Check if we have the access_token cookie
|
|
access_token = request.cookies.get("access_token_cookie")
|
|
logger.debug(f"Access token from cookies: {access_token[:20] if access_token else None}...")
|
|
|
|
if not access_token:
|
|
logger.debug("No access token found in cookies")
|
|
return None
|
|
|
|
# Decode the JWT token manually
|
|
with current_app.app_context():
|
|
try:
|
|
decoded_token = decode_token(access_token)
|
|
current_user_id = decoded_token["sub"]
|
|
logger.debug(f"Decoded user ID: {current_user_id}")
|
|
|
|
if not current_user_id:
|
|
logger.debug("No user ID in token")
|
|
return None
|
|
except Exception as e:
|
|
logger.debug(f"Token decode error: {e}")
|
|
return None
|
|
|
|
# Query database for user data
|
|
from app.models.user import User
|
|
|
|
user = User.query.get(int(current_user_id))
|
|
if not user or not user.is_active:
|
|
logger.debug(f"User not found or inactive: {current_user_id}")
|
|
return None
|
|
|
|
logger.debug(f"Successfully found user: {user.email}")
|
|
return {
|
|
"id": str(user.id),
|
|
"email": user.email,
|
|
"name": user.name,
|
|
"role": user.role,
|
|
"credits": user.credits,
|
|
}
|
|
except Exception as e:
|
|
logger.debug(f"Exception in get_user_from_socketio: {e}")
|
|
return None
|
|
|
|
|
|
@socketio.on("connect")
|
|
def handle_connect(auth=None):
|
|
"""Handle client connection."""
|
|
print(f"🔌 CONNECT EVENT HANDLER CALLED: {request.sid}")
|
|
try:
|
|
logger.info(f"SocketIO connection established from {request.remote_addr}")
|
|
logger.info(f"Session ID: {request.sid}")
|
|
logger.info(f"Cookies: {request.cookies}")
|
|
print(f"🔌 CONNECT SUCCESS: {request.sid}")
|
|
|
|
except Exception:
|
|
logger.exception("Error handling SocketIO connection")
|
|
print(f"🔌 CONNECT ERROR: {request.sid}")
|
|
disconnect()
|
|
|
|
|
|
@socketio.on("authenticate")
|
|
def handle_authenticate(data):
|
|
"""Handle authentication after connection."""
|
|
print(f"🔐 AUTHENTICATE EVENT HANDLER CALLED: {request.sid}")
|
|
logger.info(f"🔐 SOCKETIO EVENT: authenticate received from {request.sid}")
|
|
logger.info(f"🔐 Auth data: {data}")
|
|
logger.info(f"🔐 Request cookies: {dict(request.cookies)}")
|
|
|
|
try:
|
|
user = SocketIOService.get_user_from_socketio()
|
|
logger.info(f"🔐 User lookup result: {user}")
|
|
|
|
if not user:
|
|
logger.warning("🔐 Authentication failed - no user found")
|
|
emit("auth_error", {"error": "Authentication failed"})
|
|
disconnect()
|
|
return
|
|
|
|
user_id = int(user["id"])
|
|
user_room = SocketIOService.get_user_room(user_id)
|
|
|
|
# Join user-specific room
|
|
join_room(user_room)
|
|
|
|
logger.info(f"🔐 User {user_id} authenticated and joined room {user_room}")
|
|
|
|
# Send current credits on authentication
|
|
emit("auth_success", {"user": user})
|
|
emit("credits_changed", {"credits": user["credits"]})
|
|
|
|
except Exception:
|
|
logger.exception("🔐 Error handling SocketIO authentication")
|
|
emit("auth_error", {"error": "Authentication failed"})
|
|
disconnect()
|
|
|
|
|
|
@socketio.on("test_event")
|
|
def handle_test_event(data):
|
|
"""Test handler to verify SocketIO events are working."""
|
|
print(f"🧪 TEST EVENT HANDLER CALLED: {request.sid}")
|
|
logger.info(f"🧪 TEST EVENT received: {data}")
|
|
emit("test_response", {"message": "Test event received successfully"})
|
|
|
|
|
|
@socketio.on("disconnect")
|
|
def handle_disconnect():
|
|
"""Handle client disconnection."""
|
|
print(f"🔌 DISCONNECT EVENT HANDLER CALLED: {request.sid}")
|
|
try:
|
|
user = SocketIOService.get_user_from_socketio()
|
|
if user:
|
|
user_id = int(user["id"])
|
|
user_room = SocketIOService.get_user_room(user_id)
|
|
leave_room(user_room)
|
|
logger.info(f"User {user_id} disconnected from SocketIO")
|
|
|
|
except Exception:
|
|
logger.exception("Error handling SocketIO disconnection")
|
|
|
|
|
|
# Export the service instance
|
|
socketio_service = SocketIOService()
|
|
|
|
# Debug: Print registered event handlers
|
|
try:
|
|
print(f"🔧 SocketIO Service: Registered event handlers: {list(socketio.server.handlers.keys())}")
|
|
print(f"🔧 SocketIO Service: Namespace handlers: {socketio.server.handlers.get('/', {})}")
|
|
except Exception as e:
|
|
print(f"🔧 SocketIO Service: Error accessing handlers: {e}")
|
|
print(f"🔧 SocketIO Service: SocketIO server: {socketio.server}")
|
|
print(f"🔧 SocketIO Service: SocketIO instance: {socketio}")
|
|
|
|
# Test manual event registration
|
|
@socketio.on("manual_test")
|
|
def handle_manual_test(data):
|
|
"""Manual test handler."""
|
|
print(f"🧪 MANUAL TEST EVENT: {data}")
|
|
|
|
print("🔧 SocketIO Service: Manual test handler registered")
|