Use HTTP Keep-Alive for server to communicate to client

后端 未结 3 753
-上瘾入骨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:01

    With HTTP 1.1, keep-alive is the default behavior. (In HTTP 1.0, the default behavior was to close the connection.) The server must send the 'Connection: close" header to terminate the connection with the first response. So there is still a TCP socket available to push data through, but just pushing data from the server would violate the HTTP protocol in a major way. Even using keep-alive, the client would still have to poll the server.

    It is important to distinguish between HTTP Keepalive and TCP Keepalive. HTTP keepalive prevents the connection from being closed by the server or client. TCP keepalive is used when the connection might be idle for an extended period of time and might be dropped by a NAT proxy or firewall. TCP keepalive is activated on a per-socket basis by setsockopt() calls.

    When doing a 'long poll' to eliminate the need to re-poll, TCP keepalive might be needed.

提交回复
热议问题