简体   繁体   English

Web sockets 在 Http 上运行是什么意思?

[英]What does it mean that Web sockets operate over Http?

https://sookocheff.com/post/networking/how-do-websockets-work/#:~:text=WebSocket%20uses%20HTTP%20as%20the,the%20use%20of%20long%2Dpolling . https://sookocheff.com/post/networking/how-do-websockets-work/#:~:text=WebSocket%20uses%20HTTP%20as%20the,the%20use%20of%20long%2Dpolling

WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection. WebSockets 提供双向、全双工通信通道,通过单个 TCP/IP 套接字连接在 HTTP 上运行。

Http is considered unidirectional and slower than websockets so what does it mean that Web sockets operate over Http? Http 被认为是单向的,并且比 websockets 慢,所以 Web sockets 在 Z9D4D55DE68F05BBB55 上运行意味着什么

in node.js I have seen that an http server is created first and then socket.io starts piggybanking on that server.在 node.js 中,我看到首先创建了 http 服务器,然后socket.io在该服务器上开始储蓄。

Is that the only way to work?这是唯一的工作方式吗? Why is that done?为什么这样做?

Using the word "over" is a mistake.使用“over”这个词是错误的。 Websockets do not operate over HTTP. Websocket 不能在 HTTP 上运行。 That post does go on to explain the upgrade separation correctly later though.该帖子确实在稍后正确解释了升级分离。

https://tools.ietf.org/html/rfc6455#section-1.7 https://tools.ietf.org/html/rfc6455#section-1.7

The WebSocket Protocol is an independent TCP-based protocol. WebSocket 协议是一个独立的基于 TCP 的协议。 Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request.它与 HTTP 的唯一关系是它的握手被 HTTP 服务器解释为升级请求。

The socket.io library can do it's bidirectional messaging over multiple transports, websockets being one of them. socket.io 库可以通过多种传输进行双向消息传递,websockets 就是其中之一。 It also supports HTTP long polling that will be used if a websocket connection is not possible.它还支持 HTTP 长轮询,如果 websocket 连接不可行,将使用该轮询。

What socket.io is not socket.io 不是什么

Socket.IO is NOT a WebSocket implementation. Socket.IO 不是 WebSocket 实现。 Although Socket.IO indeed uses WebSocket as a transport when possible, it adds additional metadata to each packet.尽管 Socket.IO 确实尽可能使用 WebSocket 作为传输,但它会为每个数据包添加额外的元数据。 That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either

Socket.io also sets up the middleware to deliver the Javascript content for the client and handle the long polling requests when piggy backed to the http server. Socket.io 还设置中间件来为客户端提供 Javascript 内容,并在背负到http服务器时处理长轮询请求。 This makes it easier to use and means you don't need to manage a static Javascript dependency这使它更易于使用,并且意味着您不需要管理 static Javascript 依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM