title: Handshake doesn't work on Safari, but works on Chrome
body: <p>I've found a problem trying to test a websocket application. My server is written in Delphi 7, and I try to connect a client browser to it. </p>
  
  <p>I know that Chrome uses the "draft-ietf-hybi-thewebsocketprotocol-06" protocol, and Safari uses the "draft-ietf-hybi-thewebsocketprotocol-00" one. So, I've done the specifics for both.</p>
  
  <p>So, when i run the client code on Chrome, it works fine, e I got the handshake to be accepted by the browser. After that, I can send and receive messages from and to both sides. </p>
  
  <p>But, when I run the same client code with a Safari browser, it doesn't work at all. I got the handshake from the browser on my server, like this:</p>
  
  <pre><code>GET / HTTP/1.1
  #$D#$A
  Upgrade: WebSocket
  #$D#$A
  Connection: Upgrade
  #$D#$A
  Host: 192.168.0.1:5050
  #$D#$A
  Origin: http://192.168.0.1
  #$D#$A
  Cookie: PHPSESSID=87i5k8n2f2i6e05sesmgvv9q82
  #$D#$A
  Sec-WebSocket-Key1: 133r  170600  u j5
  #$D#$A
  Sec-WebSocket-Key2: 6"3 7#  "' 9 Z0v7Vo 011 :
  #$D#$A
  #$D#$A
  q¯×/­“#$11ï
  </code></pre>
  
  <p>So, my server answers:</p>
  
  <pre><code>HTTP/1.1 101 WebSocket Protocol Handshake
  #$D#$A
  Upgrade: WebSocket
  #$D#$A
  Connection: Upgrade
  #$D#$A
  Sec-WebSocket-Origin: http://192.168.0.1
  #$D#$A
  Sec-WebSocket-Location: ws://192.168.0.1:5050/
  #$D#$A
  Sec-WebSocket-Protocol: chat
  #$D#$A
  #$D#$A
  ñˆñ ÷æD‡Tí#0d+ãó-
  </code></pre>
  
  <p>It seems that it's all Ok! But the client doesn't accept the answer and closes the connection immediately.</p>
  
  <p>The first thing that came into my mind was there were something wrong with my handshake answer to Safari (draft-ietf-hybi-thewebsocketprotocol-00). But, searching on web, I found the WebSocket.org website, where they have the "Echo Test" ( <a href="http://www.websocket.org/echo.html" rel="nofollow">http://www.websocket.org/echo.html</a> ). The most curious I've found, doing the normal test, both Chrome and Safari worked fine. But, saving the sample code that there is on the page, and running it local, even connecting to the WebSocket server, Safari doesn't work too. So, I imagine that there is something different with Safari. Can anyone, please, save the code below and run it into both browsers (Safari and Chrome) and check it out if it works on both? And, does anyone know what happens?</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt;WebSocket Test&lt;/title&gt;
  
  &lt;script language="javascript" type="text/javascript"&gt;
  
  var wsUri = "ws://echo.websocket.org/";
  var output;
  
  function init()
  {
  output = document.getElementById("output");
  testWebSocket();
  }
  
  function testWebSocket() 
  {
  websocket = new WebSocket(wsUri);
  websocket.onopen = function(evt) { onOpen(evt) };
  websocket.onclose = function(evt) { onClose(evt) };
  websocket.onmessage = function(evt) { onMessage(evt) };
  websocket.onerror = function(evt) { onError(evt) };
  }
  
  function onOpen(evt) 
  {
  writeToScreen("CONNECTED");
  doSend("WebSocket rocks");
  }
  
  function onClose(evt) 
  {
  writeToScreen("DISCONNECTED");
  }
  
  function onMessage(evt) 
  {
  writeToScreen('&lt;span style="color: blue;"&gt;RESPONSE: ' + evt.data+'&lt;/span&gt;');
  websocket.close();
  }
  
  function onError(evt) {
  writeToScreen('&lt;span style="color: red;"&gt;ERROR:&lt;/span&gt; ' + evt.data);
  }
  
  function doSend(message) 
  {
  writeToScreen("SENT: " + message);
  websocket.send(message);
  }
  
  function writeToScreen(message) 
  {
  var pre = document.createElement("p");
  pre.style.wordWrap = "break-word";
  pre.innerHTML = message;
  output.appendChild(pre);
  }
  
  window.addEventListener("load", init, false);
  
  &lt;/script&gt;
  
  &lt;h2&gt;WebSocket Test&lt;/h2&gt;
  
  &lt;div id="output"&gt;&lt;/div&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks!</p>