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

Qix*_*Qix 11 javascript html5 header http websocket

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

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

可能?

连接输出:

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  ?
Run Code Online (Sandbox Code Playgroud)

Mat*_*hen 5

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.

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.

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

  • 从技术上讲,WebSocket连接**使用**TCP套接字:-).但它不是**纯/原始的**TCP套接字.TCP,UDP等是OSI第4层(传输).WebSockets在OSI第5层和第6层中更多.理论上,您可以通过UDP(或域套接字等)进行WebSocket连接,但当前的WebSockets定义在TCP上分层. (2认同)