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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4590447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:14:25  来源:igfitidea点击:

JS/HTML5 WebSocket: Connect without HTTP call

javascripthttphtmlheaderwebsocket

提问by Qix - MONICA WAS MISTREATED

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 notlike conventional sockets. That's why I'm asking here.

好吧,所以我刚刚在 HTML 5 中尝试了新的 WebSocket 类,并且对它们的存在感到非常兴奋;不过,我看不出他们是如何更有价值比AJAX看到,他们怎么还发起HTTP调用,并没有像传统的插座。这就是我在这里问的原因。

Is there a way with HTML 5's WebSocketclass 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).

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

Possible?

可能的?

Output from connection:

连接输出:

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  ?

采纳答案by Matthew Flaschen

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.

不。一旦建立连接,您就有了一个真正的套接字。但是您说得对,它需要特殊的服务器支持。因此它不会让您连接到未经修改的 POP 或 IMAP 服务器。

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.

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

There isstill 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).

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