Use HTTP Keep-Alive for server to communicate to client

后端 未结 3 769
-上瘾入骨i
-上瘾入骨i 2021-02-07 09:41

Recently in an interview I was asked how I would approach an online chat client application. I went through the standard \"polling\" solution but was cut off because the interv

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 10:17

    Keep-alive simply holds a TCP socket open, so each time you poll, you save the overhead of the TCP setup/teardown packets--but you still have to poll.

    However, "long polling" is a strategy for the web server to broadcast notifications to the client. Essentially, the client issues a GET request, but instead of immediately responding, the web server waits until they have a notification to send, at which point they respond to the GET request. This eliminates any need for packets to go across the wire for polling purposes, and keeps the connection stateless, which as you correctly mention is one of the purposes of the protocol.

提交回复
热议问题