Skip to content
Snippets Groups Projects
Commit 33618876 authored by Muhamad Nobel Fauzan's avatar Muhamad Nobel Fauzan
Browse files

add multi-user idle check

parent e43a07b2
Branches
No related merge requests found
from flask import Flask from flask import Flask
from flask_socketio import SocketIO, send from flask_socketio import SocketIO, send
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask import request
from difflib import get_close_matches from difflib import get_close_matches
import time import time
import threading import threading
...@@ -22,7 +23,7 @@ class QAPair(db.Model): ...@@ -22,7 +23,7 @@ class QAPair(db.Model):
return self.question return self.question
waiting_time = 10 waiting_time = 10
last_msg = time.time() + 12 last_msg = {}
question = QAPair.query.with_entities(QAPair.question).all() question = QAPair.query.with_entities(QAPair.question).all()
questions = [a.question for a in question] questions = [a.question for a in question]
...@@ -36,17 +37,23 @@ def handleMessage(msg): ...@@ -36,17 +37,23 @@ def handleMessage(msg):
send(ans, broadcast=False) send(ans, broadcast=False)
else: else:
send("sorry, I don't understand the question", broadcast = False) send("sorry, I don't understand the question", broadcast = False)
global last_msg global last_msg
last_msg = time.time()
last_msg[request.sid] = time.time()
def idle_check_thread(): def idle_check_thread():
global last_msg global last_msg
global waiting_time global waiting_time
while True: while True:
current_time = time.time() current_time = time.time()
if (last_msg + waiting_time) < current_time and (last_msg + waiting_time + 4) > current_time: try:
socketio.send("are you there?") for e in last_msg:
time.sleep(5) 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 = threading.Thread(target=idle_check_thread)
x.start() x.start()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment