index.js 518 B

123456789101112131415161718192021222324
  1. module.exports = function makeKillable(server) {
  2. var sockets = [];
  3. server.on('connection', function (socket) {
  4. //add socket to list
  5. sockets.push(socket);
  6. socket.once('close', function () {
  7. //remove socket from list
  8. sockets.splice(sockets.indexOf(socket), 1);
  9. });
  10. });
  11. server.kill = function (cb) {
  12. server.close(cb);
  13. sockets.forEach(function (socket) {
  14. socket.destroy();
  15. });
  16. // reset so the server can be restarted
  17. sockets = [];
  18. };
  19. return server;
  20. };