JS/HTML5 WebSocket:无需 HTTP 调用即可连接

2022-01-11 00:00:00 websocket http header javascript html

好吧,所以我刚刚试用了 HTML 5 中的新 WebSocket 类,并且对它们的存在感到非常兴奋;但是,我看不出它们如何比 AJAX 更有价值,因为它们仍然启动 HTTP 调用并且 不 像传统的套接字.这就是我在这里问的原因.

Alright so I just tried out the new WebSocket class in HTML 5, and was pretty excited they exist; however, I fail to see how they are much more rewarding than AJAX seeing as how they still initiate an HTTP call and are not like conventional sockets. That's why I'm asking here.

HTML 5 的 WebSocket 类有没有办法在不发送 HTTP 数据的情况下连接到侦听套接字?目前使用 TCP/IP 构建器,它显示了我不想要的所有这些标头废话(因为我想连接到 POP3/IMAP 服务器,而不需要像 Flash 桥或 Comet 这样的东西).

Is there a way with HTML 5's WebSocket class to connect to a listening socket without sending HTTP data? Currently with TCP/IP builder it's showing all this header crap that I don't want (since I want to connect to POP3/IMAP servers without things like Flash bridges or Comet).

可能吗?

连接输出:

Listening for connections...Connected
GET / HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:666
Origin: null
Sec-WebSocket-Key1: 2 987_390VNw60yi9
Sec-WebSocket-Key2: ~196  Y p  5    P67 428  ?

推荐答案

没有.建立连接后,您将拥有一个真正的套接字.但你是对的,它需要特殊的服务器支持.所以它不会让你连接到未经修改的 POP 或 IMAP 服务器.

No. Once the connection is established you have a true socket. But you're right that it requires special server support. So it won't let you connect to an unmodified POP or IMAP server.

他们选择了这种设计(升级机制),以便您可以轻松地拥有一个侦听 WebSocket 连接以及真正的 HTTP 请求的服务器.

They chose that design (the Upgrade mechanism) so you could easily have a server that listened for WebSocket connections as well as true HTTP requests.

与 AJAX 和 COMET 仍有很大区别.您可以使用 WebSockets 在服务器和客户端之间进行真正的全双工通信.以前的浏览器 API 没有提供这一点,迫使人们使用各种变通方法(例如重复的 AJAX 请求、COMET 的永久框架等).

There is still a big distinction from AJAX and COMET. You can use WebSockets to have true full-duplex communication between server and client. Previous browser APIs haven't provided that, forcing people to use various work-arounds (such as repeated AJAX requests, COMET's forever frame, and others).

相关文章