Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. const https = require('https');
  2. const Agent = require('agentkeepalive').HttpsAgent;
  3.  
  4. const keepaliveAgent = new Agent({
  5. maxSockets: 100,
  6. maxFreeSockets: 10,
  7. freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
  8. });
  9.  
  10. const options = {
  11. host: 'server',
  12. port: port,
  13. auth: 'username:password',
  14. path: '/path',
  15. method: 'POST',
  16. agent: keepaliveAgent,
  17. headers: {
  18. 'Accept': 'application/json'
  19. }
  20. };
  21.  
  22. makeRequest();
  23.  
  24. function makeRequest(){
  25. const req = https.request(options, res => {
  26. console.log('STATUS: ' + res.statusCode);
  27. console.log('HEADERS: ' + JSON.stringify(res.headers));
  28. res.setEncoding('utf8');
  29. res.on('data', function (chunk) {
  30. console.log('BODY: ' + chunk);
  31. });
  32. });
  33. req.on('error', e => {
  34. console.log('problem with request: ' + e.message);
  35. makeRequest();
  36. });
  37. req.end();
  38.  
  39. }
  40.  
  41. setInterval(() => {
  42. if (keepaliveAgent.statusChanged) {
  43. if(keepaliveAgent.getCurrentStatus().resetStatus != 0){
  44. keepaliveAgent.setCurrentStatus();
  45. makeRequest();
  46. }
  47. }
  48. }, 2000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement