[pion]测试你的TURN服务器
生活随笔
收集整理的這篇文章主要介紹了
[pion]测试你的TURN服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
把下面的html用瀏覽器打開,填入你想要測試的turn服務器
<html><header><title>ice測試</title> </header><body><label for="serverAddress"></label> <input id="serverAddress" value="turn:127.0.0.1:13902"/> <br/><label for="username"></label><input id="username" value="foo"/> <br/><label for="password"></label><input id="password" value="bar"/> <br/><button onclick="window.test()">Test</button><script>function checkTURNServer(turnConfig, timeout) {return new Promise(function (resolve, reject) {let promiseResolved;setTimeout(function () {if (promiseResolved) return;resolve(false);promiseResolved = true;}, timeout || 5000);promiseResolved = false;let 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 channelpc.createOffer({}).then((offer) => {if (offer.sdp.indexOf('typ relay') > -1) { // sometimes sdp contains the ice candidates...promiseResolved = true;resolve(true);}pc.setLocalDescription(offer)})pc.onicecandidate = function (ice) { //listen for candidate eventsif (promiseResolved || !ice || !ice.candidate|| !ice.candidate.candidate|| !(ice.candidate.candidate.indexOf('typ relay') > -1)) return;console.log("ice candidate=", ice.candidate)// If a relay candidate was found, notify that the TURN server works!if (ice.candidate.type === "relay") {console.log("The TURN server is reachable !");}promiseResolved = true;resolve(true);};});}window.test = () => {let serverAddress = document.getElementById("serverAddress").value;let username = document.getElementById("username").value;let password = document.getElementById("password").value;console.log("Trigger ice test. Server address=", serverAddress,", username=", username, ", password=", password)checkTURNServer({urls: serverAddress,username: username,credential: password}).then(function (bool) {console.log('is TURN server active? ', bool ? 'yes' : 'no');}).catch(console.error.bind(console));}</script> </body> </html>簡單說來,就是創建一個PeerConnection,指定ICEServer,也就是我們的目標測試ICE。
傳入本地的sdp,觸發ice過程。
onicecandidate的回調中,會收到ice的結果,如果類型是relay,則表示turn服務器正常。
總結
以上是生活随笔為你收集整理的[pion]测试你的TURN服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux命令备忘录
- 下一篇: WEBRTC TURNSERVER配置