added permissions api and websocket stuff
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
import threading
|
||||
|
||||
from flask_jwt_extended import verify_jwt_in_request, get_current_user, jwt_required, get_jwt_claims, get_jwt_identity
|
||||
from flask_login import current_user
|
||||
from flask_socketio import SocketIO, emit
|
||||
|
||||
@@ -35,17 +36,16 @@ class WebSocketBase:
|
||||
@socketio.on('connect')
|
||||
def connect_handler():
|
||||
logger.debug("new connection...")
|
||||
print(current_user)
|
||||
if current_user.is_authenticated:
|
||||
logger.debug("user is authenticated")
|
||||
print("allowed!")
|
||||
emit('my response',
|
||||
{'message': '{0} has joined'.format(current_user.name)},
|
||||
broadcast=True)
|
||||
else:
|
||||
try:
|
||||
print(verify_jwt_in_request())
|
||||
print(get_jwt_identity())
|
||||
except:
|
||||
logger.info("user is not authenticated!")
|
||||
print("not allowed!!")
|
||||
return False # not allowed here
|
||||
logger.debug("user is authenticated")
|
||||
print("allowed!")
|
||||
return True
|
||||
|
||||
@socketio.on_error()
|
||||
def handle_error(self, error):
|
||||
@@ -55,5 +55,5 @@ class WebSocketBase:
|
||||
|
||||
if __name__ == '__main__':
|
||||
wsb = WebSocketBase()
|
||||
#wsb.start_websocket_in_thread(debug=True)
|
||||
# wsb.start_websocket_in_thread(debug=True)
|
||||
wsb.start_websocket(debug=True)
|
||||
|
||||
0
backend/websocket/handlers.py
Normal file
0
backend/websocket/handlers.py
Normal file
@@ -3,19 +3,23 @@
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
|
||||
from flask import Flask
|
||||
from flask_socketio import SocketIO, emit
|
||||
|
||||
from backend import app
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'secret!'
|
||||
from socketIO_client import SocketIO, LoggingNamespace
|
||||
|
||||
logging.basicConfig()
|
||||
|
||||
|
||||
token = "# replace with: JWT Access Token"
|
||||
#print(token)
|
||||
|
||||
print("params")
|
||||
#socketIO = SocketIO('127.0.0.1', 5443, params={'jwt': '{}'.format(token)})
|
||||
print("headers")
|
||||
#socketIO = SocketIO('127.0.0.1', 5443, headers={'Authorization': 'Bearer {}'.format(token)})
|
||||
print("cookies")
|
||||
socketIO = SocketIO('127.0.0.1', 5443, cookies={'access_token_cookie': '{}'.format(token)})
|
||||
|
||||
#socketio = SocketIO(message_queue="redis://")
|
||||
socketio = SocketIO(app, port=5443, debug=True)
|
||||
socketio = SocketIO('127.0.0.1', 5443)
|
||||
|
||||
#socketio.run(app, host="localhost", port=5000)
|
||||
#socketio.init_app(app, host="localhost", port=5000, cors_allowed_origins="*", )
|
||||
@@ -24,4 +28,4 @@ socketio = SocketIO(app, port=5443, debug=True)
|
||||
socketio.emit("server_event", {'data': 42, 'msg': 'toll'})
|
||||
print("sent message!")
|
||||
socketio.emit({'data': 42, 'msg': 'toll'})
|
||||
print("sent message 2!")
|
||||
print("sent message 2!")
|
||||
|
||||
Reference in New Issue
Block a user