Created
June 27, 2021 13:14
-
-
Save charles-dr/c463e97ec82ab7a31d90f38a26691bed to your computer and use it in GitHub Desktop.
shared
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import request | |
from flask_socketio import SocketIO | |
# main app | |
from application import app | |
clients = {} | |
socketio = SocketIO(app) | |
## this is the function to notify the user through socket. | |
def notify_user(user_id, event, args): | |
if str(user_id) in clients: | |
return socketio.emit(event, args, room = clients[str(user_id)] | |
@socketio.on('connect') | |
@session_required #wrapper to check login session, which returns user info | |
def socket_connected(self): | |
print(f"[SocketConnected] user '{self.id}'") | |
clients[str(self.id)] = request.sid | |
@app.route('/connect/<to_user>') | |
def connect_to_user(to_user): | |
# do somthing before notifying to the requested user 'to_user'. | |
# then notify that a connection is requested | |
notify_user(user_id = to_user, event = 'CONNECTION_REQUESTED', args={ "message": "Connection requested!" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment