figure out how socketio works
This commit is contained in:
parent
c2cf582ca4
commit
59dfbbeca9
5 changed files with 399 additions and 3 deletions
|
@ -3,7 +3,7 @@ from typing import Any
|
|||
import dotenv
|
||||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from flask_socketio import SocketIO, emit
|
||||
from flask_socketio import SocketIO, emit, join_room, leave_room
|
||||
|
||||
from .connect import get_connection
|
||||
from .qrcode_gen import generate_qr
|
||||
|
@ -17,9 +17,10 @@ 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)
|
||||
|
@ -54,6 +55,30 @@ def ws_broadcast(event: str, *, room_id: int | None = None, data: Any) -> None:
|
|||
emit(event, final, broadcast=True, namespace="/")
|
||||
|
||||
|
||||
@socketio.on("connect")
|
||||
def handle_connection():
|
||||
print("somebody connected to socket.io", flush=True)
|
||||
|
||||
|
||||
@socketio.on("disconnect")
|
||||
def handle_disconnection():
|
||||
print("somebody disconnected from socket.io", flush=True)
|
||||
|
||||
|
||||
@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")
|
||||
def join():
|
||||
room_id = request.args.get("room")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue