🍠 Serve Your Media Without Limits From a "Potato" Computer Hosted in Mom's Basement: Take our Beloved "Series of Tubes" to Full Power
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

80 lines
2.0 KiB

<html>
<head>
<title>trickle-ice</title>
</head>
<body>
<h3> ICE Connection States </h3>
<div id="iceConnectionStates"></div> <br /><hr/>
<h3> Inbound DataChannel Messages </h3>
<div id="inboundDataChannelMessages"></div> <br /><hr/>
<h3> debug Messages </h3>
<div id="debugMessages"></div>
</body>
<script>
const isHTTPS = window.location.protocol.indexOf("https") == 0;
const socket = new WebSocket(`${isHTTPS ? "wss" : "ws"}://${window.location.host}/websocket`)
// const pc = new RTCPeerConnection({});
const pc = new RTCPeerConnection({
iceServers: [
{
urls: 'stun:stun.l.google.com:19302'
}
]
});
socket.onmessage = event => {
let msg = JSON.parse(event.data)
if (!msg) {
return console.log('failed to parse msg')
}
let el = document.createElement('p')
el.appendChild(document.createTextNode(event.data))
document.getElementById('debugMessages').appendChild(el);
if (msg.candidate) {
pc.addIceCandidate(msg)
} else {
pc.setRemoteDescription(msg)
}
}
let dc = pc.createDataChannel('data');
dc.onopen = event => {
dc.send("client connected!");
}
dc.onmessage = event => {
let el = document.createElement('p')
el.appendChild(document.createTextNode(event.data))
document.getElementById('inboundDataChannelMessages').appendChild(el);
}
pc.onicecandidate = e => {
if (e.candidate && e.candidate.candidate !== "") {
socket.send(JSON.stringify(e.candidate))
}
}
pc.oniceconnectionstatechange = () => {
let el = document.createElement('p')
el.appendChild(document.createTextNode(pc.iceConnectionState))
document.getElementById('iceConnectionStates').appendChild(el);
}
socket.onopen = () => {
pc.createOffer().then(offer => {
pc.setLocalDescription(offer)
socket.send(JSON.stringify(offer))
})
}
</script>
</html>