1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-16 08:13:30 +00:00
Files
MeshAgent/Debug/WebRTC_Test.html
2017-10-12 14:28:03 -07:00

112 lines
3.8 KiB
HTML

<html>
<head>
<title>WebRTC Test Application</title>
</head>
<body onload="start()">
<span id="statustext"></span>
<script type="text/javascript">
var configuration = { "iceServers": [] };
var connection = null;
var datachannel = null;
var currentanswer = null;
var wsocket = null;
var decoder = new TextDecoder('utf-8');
var sdp = null;
function start()
{
debug("Connecting relay, for signaling channel...");
wsocket = new WebSocket("wss://alt.meshcentral.com:443/meshrelay.ashx?id='test123'");
wsocket.binaryType = "arraybuffer";
wsocket.onopen = function (evt) { debug("Web Socket Connection established..."); }
wsocket.onmessage = function (evt)
{
if (evt.data[0] == 'c')
{
debug("Tunnel Established...");
}
else
{
var cmd = JSON.parse(decoder.decode(new Uint8Array(evt.data)));
if(cmd.cmd == 'offer')
{
debug("Received WebRTC Offer...");
startWebRTC();
var ax = null;
if (typeof mozRTCSessionDescription !== 'undefined') { ax = new mozRTCSessionDescription({ type: "offer", sdp: cmd.data }) } else { ax = new RTCSessionDescription({ type: "offer", sdp: cmd.data }) }
connection.setRemoteDescription(ax, onSetRemoteDescriptionDone, onError);
}
}
}
}
function onSetRemoteDescriptionDone()
{
connection.createAnswer(onAnswerDone, onError);
}
function onAnswerDone(answer)
{
//wsocket.send(JSON.stringify({ cmd: 'offer', data: answer.sdp }));
sdp = answer.sdp;
connection.setLocalDescription(answer, onSetLocalDescriptionDone, onError);
}
function startWebRTC()
{
if (connection != null) { debug("Error!"); return; }
if (typeof mozRTCPeerConnection !== 'undefined') { connection = new mozRTCPeerConnection(configuration); }
else if (typeof RTCPeerConnection !== 'undefined') { connection = new RTCPeerConnection(configuration); }
else if (typeof webkitRTCPeerConnection !== 'undefined') { connection = new webkitRTCPeerConnection(configuration); }
else return false;
connection.ondatachannel = onDataChannel
connection.onicecandidate = onIceCandidate;
}
function onDataChannel(event)
{
debug("Data Channel ("+ event.channel.label + ") connected");
datachannel = event.channel;
datachannel.binaryType = "arraybuffer";
datachannel.onmessage = function (msg)
{
try
{
datachannel.send(msg.data.byteLength.toString());
}
catch(e)
{
debug(e.toString());
debug(msg.data.toString());
}
};
}
function onIceCandidate(e)
{
if (e.candidate == null) {
if (sdp == null) { debug('error'); return; }
wsocket.send(JSON.stringify({ cmd: 'offer', data: sdp }));
}
else
{
sdp += ("a=" + e.candidate.candidate + "\r\n");
}
}
function onSetLocalDescriptionDone() { }
function onError(e) { if (e.message) debug(e.message); else debug(e); }
function debug(msg) { document.getElementById("statustext").innerHTML += (msg + "\r\n"); }
</script>
</body>
</html>