Merge branch 'websockets'
This commit is contained in:
commit
57e6acb1ee
5 changed files with 396 additions and 18 deletions
|
@ -1,27 +1,27 @@
|
|||
import dotenv
|
||||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, emit
|
||||
import uuid
|
||||
from dataclasses import asdict
|
||||
from flask_socketio import SocketIO, join_room, leave_room
|
||||
|
||||
from .state import State
|
||||
from .connect import get_connection
|
||||
from .room import Room
|
||||
from .song import Song, init_db, get_song_by_title_artist, add_song_in_db
|
||||
from .song_fetch import lastfm_query_search, download_song_mp3
|
||||
from .qrcode_gen import generate_qr
|
||||
|
||||
from typing import Any
|
||||
from .room import Room
|
||||
from .song import Song, add_song_in_db, get_song_by_title_artist, init_db
|
||||
from .song_fetch import download_song_mp3, lastfm_query_search
|
||||
from .state import State
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config["SECRET_KEY"] = "your_secret_key"
|
||||
socketio = SocketIO(app)
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", path="/ws")
|
||||
CORS(app)
|
||||
|
||||
|
||||
db_conn = get_connection()
|
||||
state = State(app, socketio, db_conn.cursor())
|
||||
init_db(state.db)
|
||||
|
@ -46,15 +46,28 @@ def error(msg: str, status: int = 400) -> Response:
|
|||
return res
|
||||
|
||||
|
||||
def ws_broadcast(event: str, *, room_id: int | None = None, data: Any) -> None:
|
||||
final = {}
|
||||
@socketio.on("connect")
|
||||
def handle_connection():
|
||||
print("somebody connected to socket.io", flush=True)
|
||||
|
||||
if room_id is not None:
|
||||
final["room_id"] = room_id
|
||||
|
||||
final["data"] = data
|
||||
@socketio.on("disconnect")
|
||||
def handle_disconnection():
|
||||
print("somebody disconnected from socket.io", flush=True)
|
||||
|
||||
emit(event, final, broadcast=True, namespace="/")
|
||||
|
||||
@socketio.on("join_room")
|
||||
def on_join(data):
|
||||
room = data["id"]
|
||||
join_room(room)
|
||||
print(f"somebody joined {room=}", flush=True)
|
||||
|
||||
|
||||
@socketio.on("leave_room")
|
||||
def on_leave(data):
|
||||
room = data["id"]
|
||||
leave_room(room)
|
||||
print(f"somebody left {room=}", flush=True)
|
||||
|
||||
|
||||
@app.get("/api/join")
|
||||
|
@ -98,8 +111,8 @@ def queue_next():
|
|||
if room.playing_idx >= len(room.playing):
|
||||
## queue ended
|
||||
|
||||
room.renew_queue()
|
||||
emit("update_songs", {"songs": [1, 2, 3]}, broadcast=True, namespace="/")
|
||||
# room.renew_queue()
|
||||
state.socketio.emit("update_queue", {"songs": room.playing}, to=str(room.id))
|
||||
|
||||
return {"success": True, "ended": True, "index": room.playing_idx, "queue": room.playing}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue