generated from damir/go_app_template
Add index.html
This commit is contained in:
64
index.html
Normal file
64
index.html
Normal 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>
|
||||||
Reference in New Issue
Block a user