title: WebSocket Events Not Firing
body: <p>I'm trying to create a simple WebSocket example using the HTML5/JS API. Based on what I trace out on the server, it seems like the socket is connecting, but none of the events fire (<code>onopen</code>, <code>onmessage</code>, <code>onclose</code>, etc). I'm a Flash developer so I'm not very good at debugging the JavaScript and I'm hoping someone can help me out. Here's the client side code I'm using:</p>
  <pre><code>&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;
      function startSocket()
      {
          if(&quot;WebSocket&quot; in window)
          {
              var ws = new WebSocket(&quot;ws://localhost:1740&quot;);
  
              ws.onopen = function() {
                  window.alert(&quot;open!&quot;);
              }
  
              ws.onmessage = function(event) {
                  window.alert(event.data);
              }
              
              ws.onclose = function() {
                  window.alert(&quot;Closed&quot;);
              }   
              
              ws.onerror = function() {
                  window.alert(&quot;trouble in paradise&quot;);
              }           
          }
          
      }
      
  
  &lt;/script&gt;
  </code></pre>
  <p>And here's my socket server code (which works just fine from Flash, but that may not mean anything):</p>
  <pre><code>&lt;?php
  
  create_connection('localhost',1740);
  
  function create_connection($host,$port)
  {
      $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
      
      if (!is_resource($socket)) {
          echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
      } else {
          echo &quot;Socket created.\n&quot;;
      }
      
      if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
          echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
      } else {
          echo &quot;Set options on socket.\n&quot;;
      }
      
      if (!socket_bind($socket, $host, $port)) {
          echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
      } else {
          echo &quot;Socket bound to port $port.\n&quot;;
      }
      
      if (!socket_listen($socket,SOMAXCONN)) {
          echo 'Unable to listen on socket: ' . socket_strerror(socket_last_error());
      } else {
          echo &quot;Listening on the socket.\n&quot;;
      }
      
      while (true)
      {
          $connection = @socket_accept($socket);
          
          if($connection)
          {       
              echo &quot;Client $connection connected!\n&quot;;
              send_data($connection);
  
          } else {
              echo &quot;Bad connection.&quot;;
          }
      }
  }
  
  function send_data($connection)
  {
      echo $connection;
      // Create a number between 30 and 32 that will be our initial stock price.
      $stock_price = rand(30,32);
      while (true)
      {
          socket_write($connection,&quot;$stock_price\n&quot;,strlen(&quot;$stock_price\n&quot;));
          sleep(1);
          
          // Generate a random number that will represent how much our stock price
          // will change and then make that number a decimal and attach it to the 
          // previous price.
          $stock_offset = rand(-50,50);
          $stock_price = $stock_price + ($stock_offset/100);
          echo &quot;$stock_price\n&quot;;
      }
  }
  
  
  ?&gt;
  </code></pre>