title: nodejs + WebSockets - reject the connection with a message
body: <p>I want to provide a meaningful error to the client when too many users are connected or when they're connecting from an unsupported domain, so...</p>
  <p>I wrote some WebSocket server code:</p>
  <pre><code>var http = require('http');
  var httpServer = http.createServer(function (request, response)
  {
      // i see this if i hit http://localhost:8001/
      response.end('go away');
  });
  
  httpServer.listen(8001);
  
  // https://github.com/Worlize/WebSocket-Node/wiki/Documentation
  var webSocket = require('websocket');
  var webSocketServer = new webSocket.server({ 'httpServer': httpServer });
  
  webSocketServer.on('request', function (request)
  {
      var connection = request.reject(102, 'gtfo'); 
  });
  </code></pre>
  <p>And some WebSocket client code:</p>
  <pre><code>var connection = new WebSocket('ws://127.0.0.1:8001');
  connection.onopen = function (openEvent)
  {
      alert('onopen');
      console.log(openEvent);
  };
  connection.onclose = function (closeEvent)
  {
      alert('onclose');
      console.log(closeEvent);
  }
  connection.onerror = function (errorEvent)
  {
      alert('onerror');
      console.log(errorEvent);
  };
  connection.onmessage = function (messageEvent)
  {
      alert('onmessage');
      console.log(messageEvent);
  };
  </code></pre>
  <p>All I get is <code>alert('onclose');</code> with a <code>CloseEvent</code> object logged to the console without any status code or message that I can find.  When I connect via <code>ws://localhost:8001</code> the httpServer callback doesn't come into play, so I can't catch it there.  The <a href="https://www.rfc-editor.org/rfc/rfc6455" rel="nofollow noreferrer">RFC</a> suggests I should be able to send any status code other than 101 when there's a problem, but Chrome throws an error in the console <code>Unexpected response code: 102</code>.  If I call <code>request.reject(101, 'gtfo')</code>, implying it was successful I get a handshake error, as I'd expect.</p>
  <p>Not really sure what else I can do.  Is it just not possible right now to get the server response in Chrome's WebSocket implementation?</p>
  <p><strong>ETA:</strong> Here's a really nasty hack in the mean time, I hope that's not what I have to end up doing.</p>
  <pre><code>var connection = request.accept(null, request.origin);
  connection.sendUTF('gtfo');
  connection.close();
  </code></pre>