From 3361887655f71cc94c1a0a6c0306efdc623e9331 Mon Sep 17 00:00:00 2001 From: Nobelf <13517042@std.stei.itb.ac.id> Date: Sat, 16 Apr 2022 12:10:07 +0700 Subject: [PATCH] add multi-user idle check --- app.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index c0f4e1b..231f36f 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ from flask import Flask from flask_socketio import SocketIO, send from flask_sqlalchemy import SQLAlchemy +from flask import request from difflib import get_close_matches import time import threading @@ -22,7 +23,7 @@ class QAPair(db.Model): return self.question waiting_time = 10 -last_msg = time.time() + 12 +last_msg = {} question = QAPair.query.with_entities(QAPair.question).all() questions = [a.question for a in question] @@ -36,17 +37,23 @@ def handleMessage(msg): send(ans, broadcast=False) else: send("sorry, I don't understand the question", broadcast = False) + global last_msg - last_msg = time.time() + + last_msg[request.sid] = time.time() def idle_check_thread(): global last_msg global waiting_time while True: current_time = time.time() - if (last_msg + waiting_time) < current_time and (last_msg + waiting_time + 4) > current_time: - socketio.send("are you there?") - time.sleep(5) + try: + for e in last_msg: + if (last_msg[e] + waiting_time) < current_time and (last_msg[e] + waiting_time + 4) > current_time: + socketio.send("are you there?",to= e ) + last_msg[e] = 0 + except: + pass x = threading.Thread(target=idle_check_thread) x.start() -- GitLab