問題描述
好吧,所以我剛剛試用了 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).
這篇關于JS/HTML5 WebSocket:無需 HTTP 調用即可連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!