How to implement dual communication between server and client via http

I have an AJAX client that should receive messages from the server. Some messages from the server are not similar to the type of request-response. For example, a game with an image that players can enter. The server must send the client information about the player’s entry. But how can a server send a message to a client via http without a request from the client? Only the solution I can come up with: the client sends a request to the server (getNewPlayerEnter request) with a large timeout, the server checks the status of the set of players, if new players are installed in the list, then the server sends information to the client if the server does not "sleep" in which at that time, and after the "sleeping" server, player checks are set again. I think my mood is a little stupid (maybe a little).How to implement this correctly?
ps sorry for my english

+3
source share
2 answers

What you're talking about is a “poll” when your client-side code will query the server every X seconds to check for updates.

This is an easy to implement solution, so you see it so often. However, the best solution is to use a comet.

In 2006, Alex Russell coined the term "comet" to define the way in which a client makes both an HTTP request to a server and the server keeps this connection open indefinitely. With this open connection, the server can then update the client in response to updates.

When a response is received, the client immediately opens a new, durable HTTP connection.

"Comet" , , , .

Comet Daily , - , Comet. , Firebug Chrome Debugger, / .

http://cometdaily.com/2009/10/23/concrete-comet-examples/

+5

, ping- (, 10 ). , . , - " ", .

0
source

Source: https://habr.com/ru/post/1785636/


All Articles