使用HTTP Keep-Alive服务器与客户端进行通信 [英] Use HTTP Keep-Alive for server to communicate to client

查看:375
本文介绍了使用HTTP Keep-Alive服务器与客户端进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在接受采访时,我被问到如何接近在线聊天客户端应用程序。我经历了标准的民意调查解决方案,但因为面试官正在寻找HTTP 1.1 keep-alive方法而被切断了。使用HTTP已经有一段时间并且记住整点都是无状态的,这从来没有发生在我身上(更不用说保持活着并没有一贯地实现)。

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 interviewer was looking for the "HTTP 1.1 keep-alive" method. Having used HTTP for quite a while and remembering that the whole point was to be "stateless", this never occurred to me (also, not to mention that the keep-alive is not consistently implemented).

我的问题是,当设置keep-alive标头时,网络服务器是否可以向客户端广播和/或发送信息?

My question is, is it possible for a web server to broadcast and/or send information to a client when the "keep-alive" header has been set?

推荐答案

Keep-alive只是打开一个TCP套接字,因此每次轮询时,都会节省TCP setup / teardown数据包的开销 - 但是你仍需要进行轮询。

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.

但是,长轮询是Web服务器向客户端广播通知的策略。本质上,客户端发出GET请求,但Web服务器不会立即响应,而是等待它们发送通知,此时它们会响应GET请求。这消除了为了轮询而需要通过线路的数据包,并保持连接无状态,正如您正确提到的那样是协议的目的之一。

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.

这篇关于使用HTTP Keep-Alive服务器与客户端进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆