Files
webtransport_test/index.html
2026-04-04 14:54:19 +00:00

64 lines
2.0 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>