Real time countdown implementation #1695
-
Hello! Working on typing speed test app using I have
What I achieve: implemented countdown using Celery
Question: # tasks.py
@celery.task
def start_countdown(seconds: int) -> bool:
socketio = create_socketio(flask_app, False)
while seconds > 0:
socketio.emit('typing_test_countdown', seconds)
seconds -= 1
socketio.sleep(1)
return True # events.py
@socketio.on('keydown')
def on_keydown(key: str):
cfg = session.get('config')
typing_test_finished = False
if session.get('is_started') in (None, False):
# Start typing test
session['is_started'] = True
result = start_countdown.delay(cfg.duration) # starts celery task with countdown
typing_test_finished = result.get()
emit('keydown_reply', {
'key': key,
'correct': False, # not implemented
})
else:
# Process typing test
emit('keydown_reply', {
'key': key,
'correct': False, # not implemented
})
if typing_test_finished is True:
# Send typing test results
emit('get_result', {}) # not implemented
typing_test_finished = False
session['is_started'] = False |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
My main question is why do you need the server to count down the seconds. Wouldn't it be easier for the client to count the time on its own? The server has to keep track of when the countdown expires, of course, but I don't see why it has to control the client side timer, since both can count seconds on their own and they'll be very close, if not exactly the same.
See the section on emitting from an external process in the documentation.
You can use the disconnect handler to do any necessary clean up work. This is invoked after the user refreshes the page. |
Beta Was this translation helpful? Give feedback.
My main question is why do you need the server to count down the seconds. Wouldn't it be easier for the client to count the time on its own? The server has to keep track of when the countdown expires, of course, but I don't see why it has to control the client side timer, since both can count seconds on their own and they'll be very close, if not exactly the same.
See the section on emitting from an external process in the documentation.
You can use the d…