def disconnect(sid): user = remove_session(sid) if user: trace_debug("Disconnected-->{}".format(user)) if USER_SESSION: user_list_response(sio, user.scope) else: trace_info("Disconnected with SID:: {}. No Info on DB!".format(sid))
def user_list_response(sio, scope): """ Trigger User List (online) :param sio: socket :param scope: str :return: None """ data = active_user_list(scope) sio.emit(EMIT_USER_LIST, set_json(data)) trace_info(data)
def failed_response(sio, reason, receiver, sid): """ If failed it will send response as failed event with reason key as a reason. :param sio: socket :param reason: str :param receiver: str :param sid: str :return: None """ sio.emit(EMIT_FAIL, set_json(dict(reason=reason, to=receiver)), room=sid) trace_info(reason)
def success_response(sio, reason, receiver, sid): """ For successful session connection this will trigger a success event. :param sio: socket :param reason: str :param receiver: str :param sid: str :return: None """ sio.emit(EMIT_SUCCESS, set_json(dict(reason=reason, to=receiver)), room=sid) trace_info(reason)
def update_user_online_info(sid): connect() try: user = User.get(User.sid == sid) if user: user.last_seen = datetime.now() user.is_online = False user.save() close() return user except Exception as ex: trace_info(str(ex) + " --> Exception while remove session.") close()
def send_message(sid, scope, address, message): user_session = get_session(scope, address) if user_session.sid == sid: # trace_debug("Name={}. Message={}".format(address, message)) msg = get_dict(message) receiver = get_session(scope, msg['receiver'], False) if not receiver: reason = "User you tried is not registered! ({}, {})".format( scope, address) failed_response(sio, reason, address, sid) else: raw_send_read_msg = { "txn": msg["txn"], "text": msg['text'], "sender": address, "to": receiver.address } save_message = save_send_message(receiver, msg['txn'], set_json(raw_send_read_msg)) if save_message: if receiver and get_server_socket(sio, receiver.sid): new_message_response(sio, scope, msg['txn'], msg['text'], address, receiver.address, receiver.sid) receive_ack_response(sio, msg['txn'], scope, user_session.address, sid) trace_debug( "Message received by -->{}, SID: {}, TXN: {}, MSG: {}". format(receiver.address, receiver.sid, msg['txn'], msg['text'])) else: sent_ack_response(sio, msg['txn'], scope, user_session.address, sid) trace_debug( "Message sent to -->{}, SID: {}, TXN: {}, MSG: {}". format(receiver.address, receiver.sid, msg['txn'], msg['text'])) else: failed_response( sio, "DB STORE FAILED. MSG: {}, RAW MSG: {}".format( msg, raw_send_read_msg), address, sid) else: trace_info(">>>INVALID SESSION FOR {}".format(address)) sio.disconnect(sid)
def register(sid: str, scope: str, address: str): if (no_session(sid=sid) or no_session( scope=scope, address=address)) and sid and scope and address: sid = sid.strip() scope = scope.strip() address = address.strip() if scope not in SCOPE_WHITE_LIST: failed_response(sio, "Wrong APP/Scope", address, sid) else: user_session = get_session(scope, address) if user_session and user_session.is_online == 1: try: if get_server_socket(sio, user_session.sid): sio.disconnect(user_session.sid) except KeyError as e: trace_debug( str(e) + "-->No SID available on server as {}".format( user_session.sid)) remove_session(user_session.sid) user_session = set_session(sid, scope, address) if user_session: success_response( sio, "Session created for {}".format(user_session.address), user_session.address, sid) user_list_response(sio, scope) new_message = get_user_message(user_session) if new_message: for msg in new_message: msg_dict = get_dict(msg.message) if msg.status == MESSAGE_STATUS['buyer']: if update_message_ack(msg.key, user_session): trace_debug( "ACK Done and Removed for {}. ADDRESS: {}, SID:: {}. Key:: {}" .format(msg.message, user_session.address, sid, msg.key)) buyer_receive_ack_response( sio, scope, msg.key, user_session.address, sid) else: failed_response( sio, "DB ERROR FOR ACK {}. user {}. Message Key:: {}" .format(msg.message, user_session.address, msg.key), user_session.address, user_session.sid) else: new_message_response(sio, scope, msg.key, msg_dict.get('text', None), msg_dict.get('sender', None), user_session.address, sid) receiver = get_session(scope, msg_dict.get('sender', None)) # If Sender and receiver both are available if receiver and get_server_socket(sio, receiver.sid): # send receive ack for receive to receiver receive_ack_response(sio, msg_dict['txn'], scope, receiver.address, receiver.sid) if update_message_ack(msg_dict["txn"], receiver): # send receive ack for buyer/targeted user buyer_receive_ack_response( sio, scope, msg_dict['txn'], receiver.address, receiver.sid) trace_debug( "ACK DONE and removed {} with SID:: {}, TXN:: {}" .format(receiver.address, receiver.sid, msg_dict['txn'])) else: trace_info( "---------Receiver missing check sockets-------" ) trace_info(receiver) trace_debug( "Receiver {} not found. TXN:: {}".format( msg_dict['sender'], msg_dict['txn'])) trace_debug("SESSION: {}, SID: {}".format( USER_SESSION, SESSION_SID)) else: trace_debug("No message found for {}, SID:: {}".format( address, sid)) else: failed_response( sio, "User session establishment failed for {}. Try again.". format(address), address, sid) sio.disconnect(sid) else: trace_info("USER SESSION:: {}".format(USER_SESSION)) trace_info("USER SID:: {}".format(SESSION_SID)) reason = "Invalid Request. Address: {}, Session: {}, App:: {}".format( address, sid, scope) failed_response(sio, reason, address, sid) sio.disconnect(sid)
trace_debug(reason) failed_response(sio, reason, current_user_session.address, current_user_session.sid) sio.disconnect(current_user_session.sid) else: trace_debug("ACK User not online. Details--> {}, {}, {}, {}".format( sid, scope, address, txn)) buyer_receive_ack_response(sio, scope, txn, c_address, sid) @sio.event def disconnect(sid): user = remove_session(sid) if user: trace_debug("Disconnected-->{}".format(user)) if USER_SESSION: user_list_response(sio, user.scope) else: trace_info("Disconnected with SID:: {}. No Info on DB!".format(sid)) if __name__ == '__main__': trace_info("Multiverse server starting at {}:{}".format(HOST, PORT)) try: eventlet_server(eventlet_listen((HOST, PORT)), app) except Exception as ex: trace_info(str(ex)) # https://socket.io/docs/using-multiple-nodes/