From f7d52f408a1cec8181ce1aacb2bd3f0b231a1e08 Mon Sep 17 00:00:00 2001 From: albert Date: Tue, 15 Feb 2022 14:54:56 +0000 Subject: [PATCH] first draft on socketIO integration --- server/scratchy_server/app.py | 5 +++++ server/scratchy_server/resources/roomres.py | 6 +++++- server/setup.py | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/server/scratchy_server/app.py b/server/scratchy_server/app.py index 8f08f140..a794dbad 100644 --- a/server/scratchy_server/app.py +++ b/server/scratchy_server/app.py @@ -1,6 +1,7 @@ from flask import Flask from flask_cors import CORS from flask_restful import Api +from flask_socketio import SocketIO from flask_apispec.extension import FlaskApiSpec from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin @@ -29,6 +30,7 @@ app = Flask(__name__) CORS(app) +socketio = SocketIO(app) # main configuration from config data (file) app.config.update(config_data) @@ -66,5 +68,8 @@ list(map(lambda res: app.add_url_rule(res["url"], view_func=res["ressource"].as_view(res["name"])), ressource)) list(map(lambda res: docs.register(res["ressource"], endpoint=res["name"]), ressource)) +socketio.on_namespace(RoomRes('/room/')) + +socketio.run(app, port=5000, host='0.0.0.0') logging.info("scratchy is up and ready") diff --git a/server/scratchy_server/resources/roomres.py b/server/scratchy_server/resources/roomres.py index 8d315bc1..8990b4ba 100644 --- a/server/scratchy_server/resources/roomres.py +++ b/server/scratchy_server/resources/roomres.py @@ -1,4 +1,5 @@ from flask import make_response +from flask_socketio import Namespace from flask_apispec import marshal_with, use_kwargs, doc from flask_apispec.views import MethodResource from marshmallow import fields @@ -8,7 +9,7 @@ @doc(tags=['Room']) -class RoomRes(MethodResource): +class RoomRes(MethodResource, Namespace): decorators = [validation] @marshal_with(RoomSchema, code=200) @@ -27,6 +28,9 @@ def delete(self, roomId): RoomModel.objects().get_or_404(id=roomId).delete() return make_response('', 204) + def on_get(self, *args, **kwargs): + self.get(*args, **kwargs) + @doc(tags=['Room']) class AllRoomRes(MethodResource): diff --git a/server/setup.py b/server/setup.py index 38ba2eb6..023f6112 100644 --- a/server/setup.py +++ b/server/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -# Merci Sam & Max : http://sametmax.com/creer-un-setup-py-et-mettre-sa-bibliotheque-python-en-ligne-sur-pypi/ +# Merci Sam & Max : http://sametmax2.com/creer-un-setup-py-et-mettre-sa-bibliotheque-python-en-ligne-sur-pypi/ setup( name='scratchy_server',