简体   繁体   English

Websocket握手操作

[英]Websocket handshake manipulation

In order to create a websocket connection in javascript we can use the websocket api for example: 为了在javascript中创建websocket连接,我们可以使用websocket api作为示例:

const socket = new WebSocket('ws://localhost:8080');

then the browser will initiate an handshake via http get request with those headers: 然后浏览器将通过带有以下标头的http get请求启动握手:

Upgrade: websocket
Connection: Upgrade 

and the server response : 和服务器响应:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

after that the client and server will have websocket connection opened. 之后,客户端和服务器将打开websocket连接。

So my question is if it's possible to do the handshake on other methods ? 所以我的问题是,是否可以使用其他方法进行握手? and if it's possible to take care of the handshake via javascript like xhr or something like that? 并且是否有可能通过xhr之类的javascript来处理握手?

The WebSockets handshake is only defined with GET, see the standard . WebSockets握手仅使用GET定义,请参见standard Also, since WebSockets do not fit into the request-response scheme of HTTP but essentially build a permanent connection where both server and client can write independently from each other there is no way to deal with WebSockets using XHR which expects a single response to a single request. 另外,由于WebSockets不适合HTTP的请求-响应方案,而是实质上建立了永久连接,服务器和客户端都可以彼此独立地进行写操作,因此无法使用使用XHR的WebSockets来处理对单个消息的单个响应请求。

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

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