title: Nodejs Websocket Close Event Called...Eventually
body: <p>I've been having some problems with the below code that I've pieced together.  All the events work as advertised however, when a client drops off-line without first disconnecting the close event doesn't get call right away.  If you give it a minute or so it will eventually get called.  Also, I find if I continue to send data to the client it picks up a close event faster but never right away.  Lastly, if the client gracefully disconnects, the end event is called just fine.</p>
  
  <p>I understand this is related to the other listen events like upgrade and ondata.  </p>
  
  <p>I should also state that the client is an embedded device.</p>
  
  <pre><code>client http request:
  GET /demo HTTP/1.1\r\n
  Host: example.com\r\n
  Upgrade: Websocket\r\n
  Connection: Upgrade\r\n\r\n
  
  
  //nodejs server (I'm using version 6.6)
  var http = require('http');
  var net = require('net');
  var sys = require("util");
  
  var srv = http.createServer(function (req, res){
  });
  
  srv.on('upgrade', function(req, socket, upgradeHead) {
  
    socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
                 'Upgrade: WebSocket\r\n' +
                 'Connection: Upgrade\r\n' +
                 '\r\n\r\n');
  
    sys.puts('upgraded');
  
    socket.ondata = function(data, start, end) {
      socket.write(data.toString('utf8', start, end), 'utf8'); // echo back
    }; 
  
    socket.addListener('end', function () {    
      sys.puts('end');  //works fine
    });
  
    socket.addListener('close', function () {
      sys.puts('close');  //eventually gets here
    });
  
  });
  srv.listen(3400);
  </code></pre>
  
  <p>Can anyone suggest a solution to pickup an immediate close event?  I am trying to keep this simple without use of modules.  Thanks in advance.</p>