Skip to content

Instantly share code, notes, and snippets.

@daltonnyx
Created October 17, 2017 18:41
Show Gist options
  • Save daltonnyx/93694d1c7bed208954ddc0c491f1a64e to your computer and use it in GitHub Desktop.
Save daltonnyx/93694d1c7bed208954ddc0c491f1a64e to your computer and use it in GitHub Desktop.
//https://stackoverflow.com/questions/28772212/stun-turn-server-connectivity-test
function checkTURNServer(turnConfig, timeout){
return new Promise(function(resolve, reject){
setTimeout(function(){
if(promiseResolved) return;
resolve(false);
promiseResolved = true;
}, timeout || 5000);
var promiseResolved = false
, myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection //compatibility for firefox and chrome
, pc = new myPeerConnection({iceServers:[turnConfig]})
, noop = function(){};
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(function(sdp){
if(sdp.sdp.indexOf('typ relay') > -1){ // sometimes sdp contains the ice candidates...
promiseResolved = true;
resolve(true);
}
pc.setLocalDescription(sdp, noop, noop);
}, noop); // create offer and set local description
pc.onicecandidate = function(ice){ //listen for candidate events
if(promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf('typ relay')>-1)) return;
promiseResolved = true;
resolve(true);
};
});
}
//Usage:
checkTURNServer({
url: 'turn:127.0.0.1',
username: 'test',
credential: 'test'
}).then(function(bool){
console.log('is TURN server active? ', bool? 'yes':'no');
}).catch(console.error.bind(console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment