| """
|
| SocketIO event handlers.
|
| Session validation, force logout, connection tracking.
|
| """
|
|
|
| from flask import request
|
| from auth.helpers import active_connections
|
| from database.users import load_users_db
|
|
|
|
|
| def register_socketio_events(socketio):
|
| """Register all SocketIO event handlers."""
|
|
|
| @socketio.on('connect')
|
| def handle_connect():
|
| pass
|
|
|
| @socketio.on('disconnect')
|
| def handle_disconnect():
|
| for username in list(active_connections.keys()):
|
| if request.sid in active_connections[username]:
|
| active_connections[username].remove(request.sid)
|
| if not active_connections[username]:
|
| del active_connections[username]
|
|
|
| @socketio.on('register_session')
|
| def handle_register_session(data):
|
| username = data.get('username')
|
| session_id = data.get('session_id')
|
| if username and session_id:
|
| if username not in active_connections:
|
| active_connections[username] = []
|
| if request.sid not in active_connections[username]:
|
| active_connections[username].append(request.sid)
|
|
|
| @socketio.on('check_session')
|
| def handle_check_session(data):
|
| username = data.get('username')
|
| session_id = data.get('session_id')
|
| users_db = load_users_db()
|
| if username in users_db:
|
| if users_db[username].get('session_id') != session_id:
|
| socketio.emit('force_logout', {
|
| 'message': 'تم تسجيل الدخول من جهاز آخر.'
|
| }, room=request.sid)
|
| else:
|
| socketio.emit('session_valid', {
|
| 'status': 'valid'
|
| }, room=request.sid) |