how does long polling work javascript?

前端 未结 7 1256
傲寒
傲寒 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 11:57

    You don't see how it works from that code only, because the actual difference from a regular request is done on the server.

    The Javascript just makes a regular request, but the server doesn't have to respond to the request immediately. If the server doesn't have anything worth returning (i.e. the change that the browser is waiting for hasn't happened yet), the server just waits which keeps the connection open.

    If nothing happens on the server for some time, either the client side will time out and make a new request, or the server can choose to return an empty result just to keep the flow going.

提交回复
热议问题