G
G
gcj2018-12-21 13:22:11
Node.js
gcj, 2018-12-21 13:22:11

How to implement a timer on the server side?

Hello!
The essence of the script: there is a question and a field with an answer, the person answers the questions, if correct, then proceeds to the next one.
If the user is stupid, then every 5 minutes he is given a hint.
Problem: the contingent of users is quite well-experienced, so I don’t want to implement a timer on the client side, because you can easily get the endpoint for which the hint is requested and thus execute the request without waiting 5 minutes. Therefore, it was decided to start the timer on the server side (Node.js)
Question: Tell me how best to organize this moment. Naturally, the timer should be launched separately for each user, there is an idea to come up with something with socket.io. In general, how would you do it?
Thanks in advance for any response.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
shmatuan, 2018-12-21
@shmatuan

socket.io is a good option, but only after 5 minutes, not every second
Another option is to hash the start of the timer on the server (or the end - depending on how the timer itself is implemented) with salt, save the hash on the front side and the time with salt on the server side , upon expiration of the timer on the front, send a request to the server for a hint with checking this hash

V
VoidVolker, 2018-12-21
@VoidVolker

  1. Save test start time to database
  2. Start a timer on the client
  3. After the timer expires, send a request to the server
  4. When receiving a request, the server checks the start time of the test and if the specified time has passed, it sends a response, otherwise it fails

A
Alexey, 2018-12-21
@Azperin

If questions are also spat out through the socket, then you can add an object to this socket when connecting.

{
  questionNumber: 1,
  nextTipOn: Date.now() + 300000;
}

When a question is received on the client, it starts a timer via setTimeout, which makes a request for a hint. Well, on the server it will be easy to compare the dates.
Plus, you will always have statistics on who and what issue is now deciding what can be displayed on the page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question