avi dahan
avi dahan

Reputation: 559

Websocket handshake manipulation

In order to create a websocket connection in javascript we can use the websocket api for example:

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

then the browser will initiate an handshake via http get request with those headers:

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.

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?

Upvotes: 0

Views: 1639

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123551

The WebSockets handshake is only defined with GET, see the 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.

Upvotes: 1

Related Questions