keepAlive.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var tunnel = require('../');
  2. var helper = require('./server');
  3. // Keep alive example
  4. // this example demonstrates the keepAlive option.
  5. // keepAlive will reuse the connections
  6. // note the "tunnelKeepAlive.close();" at the end.
  7. // this step is required to finish execution nicely
  8. var configA = {
  9. host: '127.0.0.1', username: process.env.USER, dstPort: 8000, localPort: 7000, // Use keepAlive:true to keep the tunnel open.
  10. keepAlive: true
  11. };
  12. var fakeServer = helper.createServer(configA.dstPort, '127.0.0.1', function () {
  13. var tunnelKeepAlive = tunnel(configA, function () {
  14. console.log('Tunnel open');
  15. helper.createClient(7000, '127.0.0.1', console.log);
  16. helper.createClient(7000, '127.0.0.1', console.log);
  17. helper.createClient(7000, '127.0.0.1', console.log).on('close', function () {
  18. helper.createClient(7000, '127.0.0.1', console.log).on('close', function () {
  19. helper.createClient(7000, '127.0.0.1', console.log).on('close', function () {
  20. setTimeout(function () {
  21. // Call tunnel.close() to shutdown the server.
  22. console.log('TRYING TO CLOSE');
  23. tunnelKeepAlive.close();
  24. }, 2000);
  25. });
  26. });
  27. });
  28. }).on('error', function (e) {
  29. console.log('error', e);
  30. });
  31. });
  32. fakeServer.unref();