0

I would like to include some ping times in an Angular2 dashboard application. I am unable to do something simple like

this.http.get('https://www.google.com').subscribe((res) => {
  Logger.debug('my response:', res);
});

because of CORS issues. I do not have an easy way to build an api endpoint on the server that can provide these as there is no real 'server'. The application is just the files being served on localhost.

I do not have the ability to add code to the host machine (a simple python node would solve all of my problems), so I am wondering if there is a way to obtain ping times from various ip addresses from within the client side application only.

Brian Leach
  • 3,974
  • 8
  • 36
  • 75
  • [Check this](http://stackoverflow.com/questions/4954741/how-to-ping-ip-addresses-using-javascript) – Condorcho Mar 16 '17 at 16:48
  • You could use remote images, they likely won't suffer from the same CORS issues, though it may be a bit trickier to compute the overall timing – Rob M. Mar 16 '17 at 16:48
  • @Condorcho I saw that while I was searching, it looks like exactly what I need to do except that I can't create that server endpoint. I cannot create anything on the server – Brian Leach Mar 16 '17 at 17:07

1 Answers1

0

You need to use a server that has CORS headers for that.

Alternatively, as mentioned in Rob's comment, you can use an image:

let t0 = performance.now()
let img = new Image()
img.onload = function() {
  alert(`${(performance.now() - t0).toFixed(2)} ms`)
}
img.src = 'https://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif'

This loads a 1x1 GIF image from Wikipedia. Parsing will have neglectable impact, but it will still not be 100% accurate. But that's the only way I can think of without having any server-side component to ping against.

geekonaut
  • 5,714
  • 2
  • 28
  • 30