Create static/script.js
Browse files- static/script.js +96 -0
static/script.js
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# client.js (modified JavaScript)
|
2 |
+
const localVideo = document.getElementById('localVideo');
|
3 |
+
const remoteVideo = document.getElementById('remoteVideo');
|
4 |
+
const callBtn = document.getElementById('callBtn');
|
5 |
+
const endCallBtn = document.getElementById('endCallBtn');
|
6 |
+
// ... other DOM elements
|
7 |
+
|
8 |
+
let peerConnection;
|
9 |
+
let localStream;
|
10 |
+
let socket;
|
11 |
+
let roomId;
|
12 |
+
let clientId;
|
13 |
+
|
14 |
+
async function init() {
|
15 |
+
try {
|
16 |
+
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
|
17 |
+
localVideo.srcObject = localStream;
|
18 |
+
roomId = uuid.v4(); // Generate a unique room ID
|
19 |
+
clientId = uuid.v4(); // Generate a unique client ID
|
20 |
+
socket = new WebSocket(`ws://localhost:8000/ws/${roomId}/${clientId}`); // Connect to the WebSocket server
|
21 |
+
socket.onmessage = handleSocketMessage;
|
22 |
+
socket.onopen = startCall; // Start call after socket opens.
|
23 |
+
setupEventListeners();
|
24 |
+
optimizerPanel.classList.remove('hidden');
|
25 |
+
applyBandwidthPreset(currentBandwidthPreset);
|
26 |
+
} catch (error) {
|
27 |
+
console.error('Initialization error:', error);
|
28 |
+
}
|
29 |
+
}
|
30 |
+
|
31 |
+
async function startCall() {
|
32 |
+
if (!localStream) {
|
33 |
+
alert('Please allow camera and microphone access first.');
|
34 |
+
return;
|
35 |
+
}
|
36 |
+
|
37 |
+
connectionModal.classList.remove('hidden');
|
38 |
+
simulateConnectionProgress();
|
39 |
+
|
40 |
+
try {
|
41 |
+
peerConnection = new RTCPeerConnection({
|
42 |
+
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
43 |
+
});
|
44 |
+
|
45 |
+
peerConnection.onicecandidate = handleICECandidate;
|
46 |
+
peerConnection.ontrack = handleTrack;
|
47 |
+
|
48 |
+
localStream.getTracks().forEach(track => peerConnection.addTrack(track, localStream));
|
49 |
+
|
50 |
+
const offer = await peerConnection.createOffer();
|
51 |
+
await peerConnection.setLocalDescription(offer);
|
52 |
+
|
53 |
+
socket.send(JSON.stringify({ type: 'offer', sdp: offer.sdp })); // Send the offer via WebSocket
|
54 |
+
|
55 |
+
} catch (error) {
|
56 |
+
console.error('Error starting call:', error);
|
57 |
+
connectionModal.classList.add('hidden');
|
58 |
+
alert('Failed to start call. Please try again.');
|
59 |
+
}
|
60 |
+
}
|
61 |
+
|
62 |
+
function handleSocketMessage(event) {
|
63 |
+
const data = JSON.parse(event.data);
|
64 |
+
|
65 |
+
if (data.type === 'offer' || data.type === 'answer') {
|
66 |
+
peerConnection.setRemoteDescription(new RTCSessionDescription(data));
|
67 |
+
if (data.type === 'offer') {
|
68 |
+
peerConnection.createAnswer()
|
69 |
+
.then(answer => peerConnection.setLocalDescription(answer))
|
70 |
+
.then(() => socket.send(JSON.stringify({ type: 'answer', sdp: peerConnection.localDescription.sdp })));
|
71 |
+
}
|
72 |
+
} else if (data.type === 'candidate') {
|
73 |
+
peerConnection.addIceCandidate(new RTCIceCandidate(data.candidate));
|
74 |
+
}
|
75 |
+
// ... rest of the socket message handling
|
76 |
+
}
|
77 |
+
|
78 |
+
function handleICECandidate(event) {
|
79 |
+
if (event.candidate) {
|
80 |
+
socket.send(JSON.stringify({ type: 'candidate', candidate: event.candidate })); // Send ICE candidate via WebSocket
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
function handleTrack(event) {
|
85 |
+
remoteVideo.srcObject = event.streams[0];
|
86 |
+
connectionStatus.textContent = 'Connected';
|
87 |
+
connectionStatus.className = 'font-medium text-green-400';
|
88 |
+
bandwidthIndicator.classList.remove('hidden');
|
89 |
+
updateBandwidthDisplay();
|
90 |
+
callBtn.classList.add('hidden');
|
91 |
+
endCallBtn.classList.remove('hidden');
|
92 |
+
connectionModal.classList.add('hidden');
|
93 |
+
isCallActive = true;
|
94 |
+
}
|
95 |
+
|
96 |
+
// ... rest of the JavaScript code (toggleVideo, toggleAudio, etc.)
|