Merge pull request #8 from sorrelbri/sj-socket

implement bidrectional socketio
This commit is contained in:
sorrelbri 2019-10-09 10:00:53 -07:00 committed by GitHub
commit 5cd715c4c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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'})