D
D
Dokma2017-10-03 08:59:58
Socket.io
Dokma, 2017-10-03 08:59:58

How to make a realtime game on laravel?

We need to make a game. The meaning is, under certain conditions, the game starts and runs for example exactly 49 seconds. During this time, users do different actions and each action should appear to all users. That is, if one clicked on the button, then such an inscription will appear on the page of the rest. Also on the page there should be a visual timer and when the page is updated, everything should be saved and continued. And when the game ends, some action should be performed. So here's the question, suggest how to implement it. While I came up with this, Laravel receives HTTP from the user and transfers it via Redis to Socket.io, and it is already in the browser. But I did not figure out how to deal with the timer. Tell

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2017-10-03
@DmitriyEntelis

The timer is on the server.

I
Ivan, 2017-10-03
@LiguidCool

Get a node and web sockets.

S
Sergey Sokolov, 2017-10-03
@sergiks

The master timer is started on the server. Rather, it counts the time from which and to which the reception of events from users takes place.
For the user, time accuracy is also important. Therefore, on the client, you need to get the exact time the game ends. To synchronize the time of the client with the server through slow, variable latency, the network has the NTP time synchronization protocol ( how NTP works ).
You will need a similar algorithm, but not setting the system time on the client, but getting the client date-time, respectively. the end of the counter on the server.
For example, on the server, the game ends at 12:34:56. Now the time on the server is 01:23:45 (suppose it is not 49 seconds, but much longer). And on the client it is now 04:21:00. The task is to get the time on the client when 12:34:56 is reached on the server.
The first ping left the client at 04:21:00, and returned with a server time of 01:23:47 3 seconds later at 04:21:03. We consider that the server's response fell on the middle of our segment of 3 seconds. From this we get the client time when the end of the game comes.
These pings can be repeated over and over again, specifying the end time. And start the countdown timer with setTimeout in a small interval less than a second, for example. 200ms. And let him show how much is left until the specified moment of the end of the game.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question