Merge pull request #7 from sorrelbri/sj-socket

sj socket
This commit is contained in:
sorrelbri 2019-10-07 14:09:55 -07:00 committed by GitHub
commit ecf75c6bcd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 11 deletions

View file

@ -1,5 +1,7 @@
from flask import Blueprint, request, jsonify, session from flask import Blueprint, request, jsonify, session
from .users.user_endpoint import UserEndpoint from .users.user_endpoint import UserEndpoint
from app import socketio
from flask_socketio import emit
api = Blueprint('api', __name__, url_prefix='/api') api = Blueprint('api', __name__, url_prefix='/api')
@ -15,3 +17,13 @@ def api_users():
@api.route('/user') @api.route('/user')
def api_user(): def api_user():
return jsonify(UserEndpoint.user()) 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'})

18
app.py
View file

@ -7,15 +7,19 @@ from configuration.config import DevelopmentConfig
from flask_bcrypt import Bcrypt from flask_bcrypt import Bcrypt
from flask_cors import CORS 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, cors_allowed_origins="http://localhost:3000")
# cors_allowed_origins="http://localhost:3000"
def create_app(): def create_app():
app = Flask(__name__) CORS(app, resources={
CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}, r"/api/*": {"origins": "http://localhost:3000"},
r"/auth/*": {"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) db.init_app(app)
ma.init_app(app) ma.init_app(app)
return app return app
bcrypt = Bcrypt(create_app())

View file

@ -14,7 +14,7 @@ class DevelopmentConfig(BaseConfig):
DEBUG = True DEBUG = True
BCRYPT_LOG_ROUNDS = 4 BCRYPT_LOG_ROUNDS = 4
SQLALCHEMY_DATABASE_URI = DATABASE SQLALCHEMY_DATABASE_URI = DATABASE
PORT = 8000 PORT = 5000
class TestingConfig(BaseConfig): class TestingConfig(BaseConfig):

View file

@ -3,14 +3,18 @@ astroid==2.3.1
bcrypt==3.1.7 bcrypt==3.1.7
cffi==1.12.3 cffi==1.12.3
Click==7.0 Click==7.0
dnspython==1.16.0
eventlet==0.25.1
Flask==1.1.1 Flask==1.1.1
Flask-Bcrypt==0.7.1 Flask-Bcrypt==0.7.1
Flask-Cors==3.0.8 Flask-Cors==3.0.8
flask-marshmallow==0.10.1 flask-marshmallow==0.10.1
Flask-Migrate==2.5.2 Flask-Migrate==2.5.2
Flask-Script==2.0.6 Flask-Script==2.0.6
Flask-SocketIO==4.2.1
Flask-SQLAlchemy==2.4.1 Flask-SQLAlchemy==2.4.1
Flask-Testing==0.7.1 Flask-Testing==0.7.1
greenlet==0.4.15
isort==4.3.21 isort==4.3.21
itsdangerous==1.1.0 itsdangerous==1.1.0
Jinja2==2.10.1 Jinja2==2.10.1
@ -20,6 +24,7 @@ MarkupSafe==1.1.1
marshmallow==3.2.0 marshmallow==3.2.0
marshmallow-sqlalchemy==0.19.0 marshmallow-sqlalchemy==0.19.0
mccabe==0.6.1 mccabe==0.6.1
monotonic==1.5
numpy==1.17.2 numpy==1.17.2
psycopg2==2.8.3 psycopg2==2.8.3
pycparser==2.19 pycparser==2.19
@ -27,6 +32,8 @@ PyJWT==1.7.1
pylint==2.4.2 pylint==2.4.2
python-dateutil==2.8.0 python-dateutil==2.8.0
python-editor==1.0.4 python-editor==1.0.4
python-engineio==3.9.3
python-socketio==4.3.1
six==1.12.0 six==1.12.0
SQLAlchemy==1.3.8 SQLAlchemy==1.3.8
typed-ast==1.4.0 typed-ast==1.4.0

View file

@ -1,4 +1,4 @@
from app import create_app, db from app import create_app, db, socketio
# Blueprints # Blueprints
from api.api import api from api.api import api
@ -14,4 +14,4 @@ if __name__ == '__main__':
app.register_blueprint(api) app.register_blueprint(api)
app.register_blueprint(auth) app.register_blueprint(auth)
migrate = Migrate(app, db) migrate = Migrate(app, db)
app.run(port=8000, debug=True) socketio.run(app)