Add index.html

This commit is contained in:
2026-04-04 14:54:19 +00:00
parent 030b16649a
commit 0d7e141fbe

64
index.html Normal file
View File

@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<head>
<title>WebRTC UDP Test</title>
</head>
<body>
<h2>WebRTC DataChannel Test (UDP-like)</h2>
<button onclick="startSession()">Connect</button>
<div id="status">Status: Offline</div>
<input type="text" id="message" placeholder="Type message...">
<button onclick="sendMessage()">Send</button>
<pre id="logs"></pre>
<script>
let pc = new RTCPeerConnection({
iceServers: [
{
urls: 'stun:88.210.53.29:3478'
},
{
urls: 'turn:88.210.53.29:3478',
username: 'shaihe',
credential: 'zaq1xsw2'
}
]
});
let dc;
async function startSession() {
dc = pc.createDataChannel("chat", { ordered: false, maxRetransmits: 0 }); // Настройка под UDP
dc.onmessage = e => log("Received: " + e.data);
dc.onopen = () => document.getElementById('status').innerText = "Status: Connected";
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
// Ждем сбора ICE
pc.onicecandidate = async (event) => {
if (event.candidate === null) {
const response = await fetch('/sdp', {
method: 'POST',
body: JSON.stringify(pc.localDescription)
});
const answer = await response.json();
await pc.setRemoteDescription(answer);
}
};
}
function sendMessage() {
const msg = document.getElementById('message').value;
if (dc && dc.readyState === "open") {
dc.send(msg);
log("Sent: " + msg);
}
}
function log(msg) {
document.getElementById('logs').innerText += msg + "\n";
}
</script>
</body>
</html>