Reputation: 659
Im trying to create a websocket server using net in Node.js. In the chrome console im getting an error that simply says "WebSocket Connection Failed!" and dosent show an error code or any other details. As far as i can tell ive don the handshake correctly, but the connection still fails anyway and im not certain why.
Heres the HTTP request my client sent (via WebSocket API) -
GET /chat HTTP/1.1
Host: localhost:3000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Linux; Android 7.0; AGS-L03) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Safari/537.36
Upgrade: websocket
Origin: http://localhost:3000
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,es;q=0.8,ar;q=0.7,hy;q=0.6,mi;q=0.5
Sec-WebSocket-Key: shhs88pGIFyzpQgczYc3uw==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
And heres the response my server sends back. Ive followed each step correctly according to the docs.
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: RDoXJ7C3/oDQLkUyHc2BFYdevY8=
I know there isnt anything wrong with the client code, since the WebSocket API handles the nitty gritty for me. There must be somthing im missing with the handshake on the servers side but, i still dont see what. I apreciate if anyone can point anything out i may have missed.
Upvotes: 1
Views: 1639
Reputation: 659
All fixed. According to the specs, each HTTP header must be folowed by a line termination (\r\n). My mistake was that in didn't add a double line break, which is supposed to come after the headers.
Upvotes: 1