我最近一直在详细研究 websockets。创建了我自己的服务器,并且有一个公共演示。我没有如此详细的经验或知识:http。 (尽管由于 websocket 请求是升级的 http 请求,所以我有一些。)
在我这边,服务器报告每次点击的详细信息。其中有一堆http keep-alive 请求。我的服务器不处理它们,因为它们不是 websocket 请求。但这引起了我的好奇心。
Websocket 的最大特点是连接保持活动状态。然后您可以双向(甚至同时)传递消息。我读到,Keep-Alive HTTP 连接是一个相对较新的发展(我不知道人们的时间有多少年,只是它只包含在最新的标准 - 1.1 - 现在真的很旧吗?)
我猜我可以假设两者之间存在行为差异,或者没有理由使用 websocket 标准?有什么区别?
I've been working with websockets lately in detail. Created my own server and there's a public demo. I don't have such detailed experience or knowledge re: http. (Although since websocket requests are upgraded http requests, I have some.)
On my end, the server reports details of each hit. Among them are a bunch of http keep-alive requests. My server doesn't handle them because they're not websocket requests. But it got my curiosity up.
The whole big thing about websockets is that the connection stays alive. Then you can pass messages in both directions (simultaneously even). I've read that the Keep-Alive HTTP connection is a relatively new development (I don't know how many years in people time, just that it's only included in the latest standard - 1.1 - is that actually old now?)
I guess I can assume that there's a behavioral difference between the two or there would have been no reason for a websocket standard? What's the difference?
发布评论
评论(3)
自 HTTP 1.0 起的 Keep Alive HTTP 标头,用于指示 HTTP 客户端希望与 HTTP 服务器保持持久连接。主要目标是消除为每个 HTTP 请求打开 TCP 连接的需要。然而,虽然有一个持久连接打开,但客户端和服务器之间的通信协议仍然遵循基本的 HTTP 请求/响应模式。换句话说,服务器端无法将数据推送到客户端。
WebSocket 是完全不同的机制,用于建立持久的全双工连接。通过这种全双工连接,服务器端可以将数据推送到客户端,并且客户端应该随时处理来自服务器端的数据。
引用维基百科上的相应条目供参考:
1) http://en.wikipedia.org/wiki/HTTP_persistent_connection
2) http://en.wikipedia.org/wiki/WebSocket
A Keep Alive HTTP header since HTTP 1.0, which is used to indicate a HTTP client would like to maintain a persistent connection with HTTP server. The main objects is to eliminate the needs for opening TCP connection for each HTTP request. However, while there is a persistent connection open, the protocol for communication between client and server is still following the basic HTTP request/response pattern. In other word, server side can't push data to client.
WebSocket is completely different mechanism, which is used to setup a persistent, full-duplex connection. With this full-duplex connection, server side can push data to client and client should be expected to process data from server side at any time.
Quoting corresponding entries on Wikipedia for reference:
1) http://en.wikipedia.org/wiki/HTTP_persistent_connection
2) http://en.wikipedia.org/wiki/WebSocket
HTTP 与 Websockets
REST (HTTP)
如果...,您可能会错误地使用 HTTP...
WebSocket
如果...,您可能会错误地使用
参考:https://blogs.windows.com/buildingapps/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2- 0/
HTTP vs Websockets
REST (HTTP)
You might be using HTTP incorrectly if…
WebSockets
You might be using WebSockets incorrectly if..
Ref: https://blogs.windows.com/buildingapps/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0/
您应该阅读 COMET,这是一种显示 HTTP Keep 限制的设计模式-活。 Keep-Alive 已经存在超过 12 年了,所以它并不是 HTTP 的新功能。问题是这还不够;客户端和服务器无法以真正的异步方式进行通信。客户端必须始终使用“挂起”请求才能从服务器返回消息;服务器可能不只是在任何需要的时候向客户端发送消息。
You should read up on COMET, a design pattern which shows the limits of HTTP Keep-Alive. Keep-Alive is over 12 years old now, so it's not a new feature of HTTP. The problem is that it's not sufficient; the client and server cannot communicate in a truly asynchronous manner. The client must always use a "hanging" request in order to get a message back from the server; the server may not just send a message to the client at any time it wants.