Compare commits
2 Commits
b17e0db2b0
...
ae238d3d18
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae238d3d18 | ||
|
|
7226d87a77 |
@@ -59,6 +59,19 @@ def play_sound(sound_id: int):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
|
# Get updated sound data to emit the new play count
|
||||||
|
sound = Sound.query.get(sound_id)
|
||||||
|
if sound:
|
||||||
|
# Emit sound_changed event to all connected clients
|
||||||
|
try:
|
||||||
|
from app.services.socketio_service import SocketIOService
|
||||||
|
SocketIOService.emit_sound_play_count_changed(sound_id, sound.play_count)
|
||||||
|
except Exception as e:
|
||||||
|
# Don't fail the request if socket emission fails
|
||||||
|
import logging
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
logger.warning(f"Failed to emit sound_play_count_changed event: {e}")
|
||||||
|
|
||||||
return jsonify({"message": "Sound playing", "sound_id": sound_id})
|
return jsonify({"message": "Sound playing", "sound_id": sound_id})
|
||||||
return (
|
return (
|
||||||
jsonify({"error": "Sound not found or cannot be played"}),
|
jsonify({"error": "Sound not found or cannot be played"}),
|
||||||
|
|||||||
@@ -46,6 +46,14 @@ class SocketIOService:
|
|||||||
{"credits": new_credits},
|
{"credits": new_credits},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def emit_sound_play_count_changed(sound_id: int, new_play_count: int) -> None:
|
||||||
|
"""Emit sound_play_count_changed event to all connected clients."""
|
||||||
|
SocketIOService.emit_to_all(
|
||||||
|
"sound_play_count_changed",
|
||||||
|
{"sound_id": sound_id, "play_count": new_play_count},
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def emit_credits_required(user_id: int, credits_needed: int) -> None:
|
def emit_credits_required(user_id: int, credits_needed: int) -> None:
|
||||||
"""Emit an event when credits are required."""
|
"""Emit an event when credits are required."""
|
||||||
@@ -155,45 +163,45 @@ def handle_authenticate(data):
|
|||||||
disconnect()
|
disconnect()
|
||||||
|
|
||||||
|
|
||||||
@socketio.on("play_sound")
|
# @socketio.on("play_sound")
|
||||||
@require_credits(1)
|
# @require_credits(1)
|
||||||
def handle_play_sound(data):
|
# def handle_play_sound(data):
|
||||||
"""Handle play_sound event from client."""
|
# """Handle play_sound event from client."""
|
||||||
try:
|
# try:
|
||||||
user = SocketIOService.get_user_from_socketio()
|
# user = SocketIOService.get_user_from_socketio()
|
||||||
|
|
||||||
if not user:
|
# if not user:
|
||||||
logger.warning("SocketIO play_sound failed - no authenticated user")
|
# logger.warning("SocketIO play_sound failed - no authenticated user")
|
||||||
# emit("error", {"message": "Authentication required"})
|
# # emit("error", {"message": "Authentication required"})
|
||||||
return
|
# return
|
||||||
|
|
||||||
user_id = int(user["id"])
|
# user_id = int(user["id"])
|
||||||
sound_id = data.get("soundId")
|
# sound_id = data.get("soundId")
|
||||||
if not sound_id:
|
# if not sound_id:
|
||||||
logger.warning("SocketIO play_sound failed - no soundId provided")
|
# logger.warning("SocketIO play_sound failed - no soundId provided")
|
||||||
SocketIOService.emit_to_user(
|
# SocketIOService.emit_to_user(
|
||||||
user_id, "error", {"message": "Sound ID required"}
|
# user_id, "error", {"message": "Sound ID required"}
|
||||||
)
|
# )
|
||||||
return
|
# return
|
||||||
|
|
||||||
# Import and use the VLC service to play the sound
|
# # Import and use the VLC service to play the sound
|
||||||
from app.services.vlc_service import vlc_service
|
# from app.services.vlc_service import vlc_service
|
||||||
|
|
||||||
logger.info(f"User {user_id} playing sound {sound_id} via SocketIO")
|
# logger.info(f"User {user_id} playing sound {sound_id} via SocketIO")
|
||||||
|
|
||||||
# Play the sound using the VLC service
|
# # Play the sound using the VLC service
|
||||||
success = vlc_service.play_sound(sound_id, user_id)
|
# success = vlc_service.play_sound(sound_id, user_id)
|
||||||
|
|
||||||
if not success:
|
# if not success:
|
||||||
SocketIOService.emit_to_user(
|
# SocketIOService.emit_to_user(
|
||||||
user_id,
|
# user_id,
|
||||||
"error",
|
# "error",
|
||||||
{"message": f"Failed to play sound {sound_id}"},
|
# {"message": f"Failed to play sound {sound_id}"},
|
||||||
)
|
# )
|
||||||
|
|
||||||
except Exception as e:
|
# except Exception as e:
|
||||||
logger.exception(f"Error handling play_sound event: {e}")
|
# logger.exception(f"Error handling play_sound event: {e}")
|
||||||
# emit("error", {"message": "Failed to play sound"})
|
# # emit("error", {"message": "Failed to play sound"})
|
||||||
|
|
||||||
|
|
||||||
@socketio.on("disconnect")
|
@socketio.on("disconnect")
|
||||||
|
|||||||
Reference in New Issue
Block a user