implement bidrectional socketio

This commit is contained in:
Sorrel Bri 2019-10-07 16:28:11 -07:00
parent 73a7373478
commit 2daacc05c9
4 changed files with 22 additions and 14 deletions

View file

@ -1,7 +1,5 @@
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')
@ -17,13 +15,3 @@ 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'})

View file

@ -1,9 +1,12 @@
from app import create_app, db, socketio from app import create_app, db
# Blueprints # Blueprints
from api.api import api from api.api import api
from auth.auth import auth from auth.auth import auth
# Web sockets
from websockets.socket import socketio
import configuration.models_mount import configuration.models_mount
from flask_migrate import Migrate from flask_migrate import Migrate
@ -14,4 +17,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)
socketio.run(app) socketio.run(app, debug=True)

0
websockets/__init__.py Normal file
View file

17
websockets/socket.py Normal file
View file

@ -0,0 +1,17 @@
from app import socketio
from flask_socketio import send, emit
@socketio.on('connect')
def handle_connection():
print('''
wow, there was a socketio connection!
cool
''')
send({'data':'connection'})
@socketio.on('message')
def handle_message(message):
print(message)
emit('message return', {'data':'a message was sent'})