Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. function checkTURNServer(turnConfig, timeout){
  2.  
  3. return new Promise(function(resolve, reject){
  4.  
  5. setTimeout(function(){
  6. if(promiseResolved) return;
  7. resolve(false);
  8. promiseResolved = true;
  9. }, timeout || 5000);
  10.  
  11. var promiseResolved = false
  12. , myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection //compatibility for firefox and chrome
  13. , pc = new myPeerConnection({iceServers:[turnConfig]})
  14. , noop = function(){};
  15. pc.createDataChannel(""); //create a bogus data channel
  16. pc.createOffer(function(sdp){
  17. if(sdp.sdp.indexOf('typ relay') > -1){ // sometimes sdp contains the ice candidates...
  18. promiseResolved = true;
  19. resolve(true);
  20. }
  21. pc.setLocalDescription(sdp, noop, noop);
  22. }, noop); // create offer and set local description
  23. pc.onicecandidate = function(ice){ //listen for candidate events
  24. if(promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf('typ relay')>-1)) return;
  25. promiseResolved = true;
  26. resolve(true);
  27. };
  28. });
  29. }
  30.  
  31. const USERNAME="test"
  32. const PASSWORD="test"
  33. const PORT=3478
  34. const IP="134.255.233.132" // you will have to change this
  35.  
  36. console.log('TURN server reachable on TCP?', await checkTURNServer( {
  37. url: `turn:${IP}:${PORT}?transport=tcp`,
  38. username: USERNAME,
  39. credential: PASSWORD,
  40. }))
  41.  
  42. console.log('TURN server reachable on UDP?', await checkTURNServer( {
  43. url: `turn:${IP}:${PORT}?transport=udp`,
  44. username: USERNAME,
  45. credential: PASSWORD,
  46. }))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement