spdy-http2 / node-spdy

SPDY server on Node.js
2.81k stars 196 forks source link

Detecting socket close events on the client. #258

Open AdamMagaluk opened 8 years ago

AdamMagaluk commented 8 years ago

I'm using spdy.createAgent method to create a http agent that im reusing for a number of outbound http requests to a server. This is working great but i can't figure out how to handle cases when the tcp connection to the server disconnects after it's been established so i can reconnect and start over.

I can only seem to get the error event on the agent to fire if i start the client when the server if already down. If i start the client with a working server then disconnect the server the client will continue to try and make requests without any errors from either the agent or client request.

Any suggestions?

See example:

var spdy = require('spdy');
var http = require('http');

var agent = spdy.createAgent({
  host: 'localhost',
  port: 1337,

  // Optional SPDY options
  spdy: {
    plain: true,
    ssl: false
  }
}).once('error', function (err) {
  console.log('agent:', err);
});

setInterval(req, 100);

function req() {
  console.log('making req')
  var options = {
    method: 'GET',
    port: 1337,
    agent: agent,
    path: '/'
  };

  var req = http.request(options, function(response) {
    console.log('yikes');
  });

  req.once('error', function(err) {
    console.log('req:', err)
  });

  req.end();
}
mpech commented 7 years ago

two months later, same pb. I am considering using

var agent = spdy.createAgent({
...
});

//intuition: once agent is connected, connection is not null but a proper object
agent.on('_connect', function(){
 //we should not be doing this as _spdyState is meant to be an internal to the library...
  agent._spdyState.connection.on('close', function(){
    console.log('closed connection');
  })
})