Reputation: 498
I am building a Web-socket Server (huge headache for me) and its turning out to be a lot harder than I expected.
Currently, I am having a problem with getting chrome to accept the handshake. For some reason it works fine in Firefox, but in chrome - The connection closes right away.
The Client Handshake
GET /chat HTTP/1.1
GET /chat HTTP/1.1
Host: localhost:8181
Host: localhost:8181
Connection: Upgrade
Connection: Upgrade
Pragma: no-cache
Pragma: no-cache
Cache-Control: no-cache
Cache-Control: no-cache
Upgrade: websocket
Upgrade: websocket
Origin: http://localhost:8080
Origin: http://localhost:8080
Sec-WebSocket-Version: 13
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: ANzq7Z0GL4lfvw518WOnig==
Sec-WebSocket-Key: ANzq7Z0GL4lfvw518WOnig==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
and my response looks like this:
String response = "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
"Upgrade: websocket" + Environment.NewLine +
"Connection: Upgrade" + Environment.NewLine +
"Sec-WebSocket-Accept: " + wsAccept + Environment.NewLine +
"Sec-WebSocket-Protocol: " + protocol + Environment.NewLine +
Environment.NewLine;
where wsAccept = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")))
and protocol = "chat";
Is there any website or anything I can use as a reference? I feel like im drawing a blank here.
Any help is greatly appreciated.
Thanks
SOLVED
I finlly got how to do it. I was sending to many parameters back, the last
"Sec-WebSocket-Protocol: " + protocol
was redundant! So the handshake should look like this:
String response = "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
"Upgrade: websocket" + Environment.NewLine +
"Connection: Upgrade" + Environment.NewLine +
"Sec-WebSocket-Accept: " + wsAccept + Environment.NewLine +
Environment.NewLine;
Hope this helps anyone facing a similar concern!
Upvotes: 0
Views: 1835
Reputation: 498
String response = "HTTP/1.1 101 Switching Protocols" + Environment.NewLine +
"Upgrade: websocket" + Environment.NewLine +
"Connection: Upgrade" + Environment.NewLine +
"Sec-WebSocket-Accept: " + wsAccept + Environment.NewLine +
Environment.NewLine;
Too many attributes was sent before. Works on iOS/8.1, Chrome/40 and Firefox/35.
Upvotes: 0
Reputation: 35885
You are missing the Sec-WebSocket-Accept
response header.
Take a look at http://en.wikipedia.org/wiki/WebSocket#WebSocket_protocol_handshake
The client sends a Sec-WebSocket-Key which is a random value that has been base64 encoded. To form a response, the GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 is appended to this base64 encoded key. The base64 encoded key will not be decoded first.[10] The resulting string is then hashed with SHA-1, then base64 encoded. Finally, the resulting reply occurs in the header Sec-WebSocket-Accept.
Read:
https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_servers
and
https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_server#Handshaking
Upvotes: 1