From 1f877fddbe48ffbd65511ae66f0b7008b09d0126 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Mon, 7 Oct 2019 10:09:49 -0700 Subject: [PATCH 1/3] init socketio --- app.py | 12 +++++++----- requirements.txt | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 4a7d7c9..4e9ec07 100644 --- a/app.py +++ b/app.py @@ -7,15 +7,17 @@ from configuration.config import DevelopmentConfig from flask_bcrypt import Bcrypt from flask_cors import CORS +from flask_socketio import SocketIO + +app = Flask(__name__) +bcrypt = Bcrypt(app) +app.config['CORS_HEADERS'] = 'Content-Type' +app.config.from_object(DevelopmentConfig) +socketio = SocketIO(app) def create_app(): - app = Flask(__name__) CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}, r"/auth/*": {"origins": "http://localhost:3000"}}) - app.config['CORS_HEADERS'] = 'Content-Type' - app.config.from_object(DevelopmentConfig) db.init_app(app) ma.init_app(app) return app - -bcrypt = Bcrypt(create_app()) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index fab2ace..e4a0a43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ Flask-Cors==3.0.8 flask-marshmallow==0.10.1 Flask-Migrate==2.5.2 Flask-Script==2.0.6 +Flask-SocketIO==4.2.1 Flask-SQLAlchemy==2.4.1 Flask-Testing==0.7.1 isort==4.3.21 @@ -27,6 +28,8 @@ PyJWT==1.7.1 pylint==2.4.2 python-dateutil==2.8.0 python-editor==1.0.4 +python-engineio==3.9.3 +python-socketio==4.3.1 six==1.12.0 SQLAlchemy==1.3.8 typed-ast==1.4.0 From 44d365199f906c17084419d883c28cab6ecb3c4e Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Mon, 7 Oct 2019 10:19:48 -0700 Subject: [PATCH 2/3] confirm socketio run app --- app.py | 6 ++++-- requirements.txt | 2 ++ server.py | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 4e9ec07..c0d99e9 100644 --- a/app.py +++ b/app.py @@ -16,8 +16,10 @@ app.config.from_object(DevelopmentConfig) socketio = SocketIO(app) def create_app(): - CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}, - r"/auth/*": {"origins": "http://localhost:3000"}}) + CORS(app, resources={ + r"/api/*": {"origins": "http://localhost:3000"}, + r"/auth/*": {"origins": "http://localhost:3000"} + }) db.init_app(app) ma.init_app(app) return app diff --git a/requirements.txt b/requirements.txt index e4a0a43..0af54ec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,8 @@ Flask-Script==2.0.6 Flask-SocketIO==4.2.1 Flask-SQLAlchemy==2.4.1 Flask-Testing==0.7.1 +gevent==1.4.0 +greenlet==0.4.15 isort==4.3.21 itsdangerous==1.1.0 Jinja2==2.10.1 diff --git a/server.py b/server.py index d38827c..23b7e45 100644 --- a/server.py +++ b/server.py @@ -1,4 +1,4 @@ -from app import create_app, db +from app import create_app, db, socketio # Blueprints from api.api import api @@ -14,4 +14,4 @@ if __name__ == '__main__': app.register_blueprint(api) app.register_blueprint(auth) migrate = Migrate(app, db) - app.run(port=8000, debug=True) \ No newline at end of file + socketio.run(app) \ No newline at end of file From 73a7373478489352ce331148fe02c92f42cd1d84 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Mon, 7 Oct 2019 13:55:43 -0700 Subject: [PATCH 3/3] establish initial socket connection --- api/api.py | 14 +++++++++++++- app.py | 6 +++--- configuration/config.py | 2 +- requirements.txt | 4 +++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/api/api.py b/api/api.py index 59fe130..6c795b8 100644 --- a/api/api.py +++ b/api/api.py @@ -1,5 +1,7 @@ from flask import Blueprint, request, jsonify, session from .users.user_endpoint import UserEndpoint +from app import socketio +from flask_socketio import emit api = Blueprint('api', __name__, url_prefix='/api') @@ -14,4 +16,14 @@ def api_users(): @api.route('/user') def api_user(): - return jsonify(UserEndpoint.user()) \ No newline at end of file + return jsonify(UserEndpoint.user()) + +@socketio.on('connect') +def handle_connection(): + print('connected') + socketio.emit('connect', payload={'data':'connection'}) + +@socketio.on('message') +def handle_message(message): + print(message) + emit('message return', {'data':'a message was sent'}) \ No newline at end of file diff --git a/app.py b/app.py index c0d99e9..fe753db 100644 --- a/app.py +++ b/app.py @@ -13,12 +13,12 @@ app = Flask(__name__) bcrypt = Bcrypt(app) app.config['CORS_HEADERS'] = 'Content-Type' app.config.from_object(DevelopmentConfig) -socketio = SocketIO(app) - +socketio = SocketIO(app, cors_allowed_origins="http://localhost:3000") +# cors_allowed_origins="http://localhost:3000" def create_app(): CORS(app, resources={ r"/api/*": {"origins": "http://localhost:3000"}, - r"/auth/*": {"origins": "http://localhost:3000"} + r"/auth/*": {"origins": "http://localhost:3000"}, }) db.init_app(app) ma.init_app(app) diff --git a/configuration/config.py b/configuration/config.py index 55c5173..4912a7b 100644 --- a/configuration/config.py +++ b/configuration/config.py @@ -14,7 +14,7 @@ class DevelopmentConfig(BaseConfig): DEBUG = True BCRYPT_LOG_ROUNDS = 4 SQLALCHEMY_DATABASE_URI = DATABASE - PORT = 8000 + PORT = 5000 class TestingConfig(BaseConfig): diff --git a/requirements.txt b/requirements.txt index 0af54ec..432ba18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,8 @@ astroid==2.3.1 bcrypt==3.1.7 cffi==1.12.3 Click==7.0 +dnspython==1.16.0 +eventlet==0.25.1 Flask==1.1.1 Flask-Bcrypt==0.7.1 Flask-Cors==3.0.8 @@ -12,7 +14,6 @@ Flask-Script==2.0.6 Flask-SocketIO==4.2.1 Flask-SQLAlchemy==2.4.1 Flask-Testing==0.7.1 -gevent==1.4.0 greenlet==0.4.15 isort==4.3.21 itsdangerous==1.1.0 @@ -23,6 +24,7 @@ MarkupSafe==1.1.1 marshmallow==3.2.0 marshmallow-sqlalchemy==0.19.0 mccabe==0.6.1 +monotonic==1.5 numpy==1.17.2 psycopg2==2.8.3 pycparser==2.19