how does long polling work javascript?

前端 未结 7 1237
傲寒
傲寒 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:55

    I think what is making this confusing to understand is that the discussion is focused on the client-side programming.

    Long-polling is not strictly a client-side pattern, but requires the web server to keep the connection open.

    Background: Client wants to be notified by web server when something occurs or is available, for example, let me know when a new email arrives without me having to go back and ask every few seconds.

    1. Client opens a connection to a specific URL on the web server.
    2. Server accepts connection, opens a socket and dispatches control to whatever server-side code handles this connection (say a servlet or jsp in java, or a route in RoR or node/express).
    3. Server code waits until the event or information is available. For example, when an email arrives, sees if any of the "waiting connections" are for the particular inbox. If they are, then respond with the appropriate data.
    4. Client receives data, does its thing, then starts another request to poll.

提交回复
热议问题