HTML rtc
Created By: chatGPT
To create real-time communication capabilities in a web application, WebRTC (Web Real-Time Communication) is used. This technology enables peer-to-peer connections allowing audio, video, and data sharing between users. Here’s a basic overview of how to use WebRTC with HTML and JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebRTC Example</title>
</head>
<body>
<h1>WebRTC Demo</h1>
<video id="localVideo" autoplay muted></video>
<video id="remoteVideo" autoplay></video>
<script>
const localVideo = document.getElementById('localVideo');
const remoteVideo = document.getElementById('remoteVideo');
let localStream;
let peerConnection;
const configuration = {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' }
]
};
// Access local media
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => {
localStream = stream;
localVideo.srcObject = stream;
// Do signaling to connect peers here
})
.catch(error => {
console.error('Error accessing media devices.', error);
});
// Create a new RTCPeerConnection
peerConnection = new RTCPeerConnection(configuration);
// Add tracks from local stream to the RTCPeerConnection
localStream.getTracks().forEach(track => {
peerConnection.addTrack(track, localStream);
});
// Handle incoming tracks
peerConnection.ontrack = event => {
remoteVideo.srcObject = event.streams[0];
};
</script>
</body>
</html>
This simple example demonstrates how to:
- Capture local audio and video from the user's device.
- Display the captured media on a local video element.
- Set up a RTCPeerConnection to handle the media sharing between peers.
You need to implement a signaling mechanism to exchange connection information between peers in order to establish a connection. For a complete solution, consider using WebSocket for signaling.
// Sample WebSocket signaling code (server-side)
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3000 });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
// Broadcast incoming message to all clients
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
});
In your signaling process, you will send and receive ICE candidates and SDP (Session Description Protocol) offers and answers, which are crucial for establishing and maintaining the peer connection. Make sure to handle the various WebRTC events appropriately for a smoother user experience.
peerConnection.onicecandidate = event => {
if (event.candidate) {
// Send ICE candidates to the remote peer through signaling
sendMessage({ 'ice': event.candidate });
}
};
// When receiving an SDP offer
peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
peerConnection.createAnswer()
.then(answer => {
return peerConnection.setLocalDescription(answer);
}).then(() => {
// Send the answer to the remote peer
sendMessage({ 'answer': peerConnection.localDescription });
});