@@ -16,12 +16,13 @@ <h1>Video Call</h1>
1616 < script src ="https://cdn.jsdelivr.net/npm/peerjs@1.3.2/dist/peerjs.min.js "> </ script >
1717 < script >
1818 const localVideo = document . getElementById ( 'localVideo' ) ;
19- const remoteVideo = document . getElementById ( 'remoteVideo ' ) ;
19+ const remoteVideos = document . getElementById ( 'remoteVideos ' ) ;
2020 const joinButton = document . getElementById ( 'joinButton' ) ;
2121 const roomIdInput = document . getElementById ( 'roomId' ) ;
2222
2323 let localStream ;
2424 let peer ;
25+ let connections = [ ] ;
2526
2627 joinButton . onclick = async ( ) => {
2728 const roomId = roomIdInput . value ;
@@ -37,23 +38,37 @@ <h1>Video Call</h1>
3738 // Initialize PeerJS
3839 peer = new Peer ( ) ;
3940
40- // Listen for call event
41- peer . on ( 'call' , call => {
42- call . answer ( localStream ) ; // Answer the call with our local stream
43- call . on ( 'stream' , stream => {
44- remoteVideo . srcObject = stream ; // Show stream in remote video element
45- } ) ;
41+ peer . on ( 'open' , id => {
42+ // Connect to the signaling server (use WebSocket, Firebase, etc.)
43+ // For simplicity, we assume there's a way to get a list of peers in the room
44+ fetch ( `https://your-server.com/peers-in-room?roomId=${ roomId } ` )
45+ . then ( response => response . json ( ) )
46+ . then ( peers => {
47+ peers . forEach ( peerId => {
48+ if ( peerId !== id ) {
49+ const call = peer . call ( peerId , localStream ) ;
50+ call . on ( 'stream' , addRemoteStream ) ;
51+ connections . push ( call ) ;
52+ }
53+ } ) ;
54+ } ) ;
4655 } ) ;
4756
48- // Connect to the room
49- const conn = peer . connect ( roomId ) ;
50- conn . on ( 'open' , ( ) => {
51- const call = peer . call ( roomId , localStream ) ;
52- call . on ( 'stream' , stream => {
53- remoteVideo . srcObject = stream ; // Show stream in remote video element
54- } ) ;
57+ // Listen for incoming calls
58+ peer . on ( 'call' , call => {
59+ call . answer ( localStream ) ; // Answer the call with our local stream
60+ call . on ( 'stream' , addRemoteStream ) ;
61+ connections . push ( call ) ;
5562 } ) ;
5663 } ;
64+
65+ function addRemoteStream ( stream ) {
66+ const videoElement = document . createElement ( 'video' ) ;
67+ videoElement . srcObject = stream ;
68+ videoElement . autoplay = true ;
69+ videoElement . playsInline = true ;
70+ remoteVideos . appendChild ( videoElement ) ;
71+ }
5772 </ script >
5873</ body >
59- </ html >
74+ </ html >
0 commit comments