title: How do I catch a WebSocket connection interruption?
body: <p>In Firefox (at least), if you hit ESC, then it will close all open WebSockets connections.
  I need to capture that disconnection and try to re-connect once it's available again.</p>
  
  <p>Here's an example of the code I've tried to implement, but nothing I can figure out will catch the error and allow me to handle it gracefully.</p>
  
  <p>Have a look at the code: <a href="http://jsfiddle.net/w5aAK/">http://jsfiddle.net/w5aAK/</a> </p>
  
  <pre><code>var url = "ws://echo.websocket.org";
      try {
          socket = window['MozWebSocket'] ? new MozWebSocket(url) : new WebSocket(url);
          socket.onopen = function(){
              console.log('Socket is now open.');
          };
          socket.onerror = function (error) {
              console.error('There was an un-identified Web Socket error');
          };
          socket.onmessage = function (message) {
              console.info("Message: %o", message.data);
          };
      } catch (e) {
          console.error('Sorry, the web socket at "%s" is un-available', url);
      }
  
  setTimeout(function(){
      socket.send("Hello World");
  }, 1000);
  </code></pre>
  
  <p>Turn on your console and watch the output.</p>
  
  <p>Am I doing something wrong here, or is it just not possible because the connection is running outside of the scope of the JS script?</p>
  
  <p>Any input would be helpful.</p>
  
  <p>Thanks!</p>