title: Chrome Websocket Handshake
body: <p>I am building a Web-socket Server (huge headache for me) and its turning out to be a lot harder than I expected. </p>
  
  <p>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. </p>
  
  <p>The Client Handshake</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>and my response looks like this: </p>
  
  <pre><code>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;
  </code></pre>
  
  <p>where <code>wsAccept = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")))</code></p>
  
  <p>and <code>protocol = "chat";</code></p>
  
  <p>Is there any website or anything I can use as a reference? I feel like im drawing a blank here.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>Thanks</p>
  
  <p><strong>SOLVED</strong></p>
  
  <p>I finlly got how to do it. I was sending to many parameters back, the last </p>
  
  <pre><code>"Sec-WebSocket-Protocol: " + protocol 
  </code></pre>
  
  <p>was redundant! So the handshake should look like this:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Hope this helps anyone facing a similar concern!</p>