how does long polling work javascript?

前端 未结 7 1251
傲寒
傲寒 2020-12-08 11:41

Hi I understand that in long polling you keep the connection with the server open for long till you a get a response back from the server and then poll again and wait for th

7条回答
  •  感动是毒
    2020-12-08 12:03

    The connection is not kept open all the time. It is closed automatically when the response is received from the server and server closes the connection. In long polling the server is not supposed to send back data immediately. On ajax complete (when server closes the connection) the new request is sent to the server, which opens a new connection again and starts to keep pending for new response.

    As was mentioned, long polling process is handled not only by client side, but mainly by server side. And not only by server script (in case of PHP), but by server itself, which doesn't close the "hanged" connection by timeout.

    FWIW, WebSockets use constantly opened connection with the server side, which makes possible to receive and send back the data without closing the connection.

提交回复
热议问题