reconnect.js 557 B

1234567891011121314151617181920212223242526272829
  1. var net = require('net'),
  2. nssocket = require('../lib/nssocket');
  3. net.createServer(function (socket) {
  4. //
  5. // Close the underlying socket after `1000ms`
  6. //
  7. setTimeout(function () {
  8. socket.destroy();
  9. }, 1000);
  10. }).listen(8345);
  11. //
  12. // Create an NsSocket instance with reconnect enabled
  13. //
  14. var socket = new nssocket.NsSocket({
  15. reconnect: true,
  16. type: 'tcp4',
  17. });
  18. socket.on('start', function () {
  19. //
  20. // The socket will emit this event periodically
  21. // as it attempts to reconnect
  22. //
  23. console.dir('start');
  24. });
  25. socket.connect(8345);