From a5bba9d3a4fd130bbad3035bebd8cdf1b80118e4 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Tue, 3 Jan 2023 16:01:33 +0100 Subject: [PATCH] create socketio routes --- truthseeker/__init__.py | 6 ++---- truthseeker/routes/routes_socketio.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 truthseeker/routes/routes_socketio.py diff --git a/truthseeker/__init__.py b/truthseeker/__init__.py index 6e8411b..14cbd3a 100644 --- a/truthseeker/__init__.py +++ b/truthseeker/__init__.py @@ -1,8 +1,6 @@ import flask -import os - -from truthseeker.routes import routes_api, routes_ui from flask_socketio import SocketIO +import os class TruthSeekerApp(flask.Flask): @@ -34,7 +32,7 @@ class TruthSeekerApp(flask.Flask): APP = TruthSeekerApp() -from truthseeker.routes import routes_api, routes_ui +from truthseeker.routes import routes_api, routes_ui, routes_socketio APP.register_blueprint(routes_api.routes_api, url_prefix="/api/v1") APP.register_blueprint(routes_ui.routes_ui, url_prefix="/") diff --git a/truthseeker/routes/routes_socketio.py b/truthseeker/routes/routes_socketio.py new file mode 100644 index 0000000..d94816e --- /dev/null +++ b/truthseeker/routes/routes_socketio.py @@ -0,0 +1,19 @@ +from flask_socketio import join_room +import socketio + +from truthseeker import APP +from truthseeker.logic import game_logic + +@APP.socketio_app.on('connect') +def connect(auth): + if not (auth and "game_id" in auth): + raise socketio.exceptions.ConnectionRefusedError("Invalid connection data passed") + + game = game_logic.get_game(auth["game_id"]) + if not game: + raise socketio.exceptions.ConnectionRefusedError("No game with this ID") + + room = join_room("game."+auth["game_id"]) + join_room(room) + +