@@ -51,11 +51,14 @@ export default function MeetingRoom() {
5151 } = useWebRTC ( {
5252 onIceCandidate : ( clientId : string , candidate : RTCIceCandidate ) => {
5353 if ( sendMessageRef . current ) {
54+ console . log ( 'Sending ICE candidate to:' , clientId ) ;
5455 sendMessageRef . current ( {
5556 type : WSMessageType . ICE_CANDIDATE ,
5657 target : clientId ,
5758 data : candidate . toJSON ( ) ,
5859 } ) ;
60+ } else {
61+ console . warn ( 'sendMessageRef is null, cannot send ICE candidate to:' , clientId ) ;
5962 }
6063 } ,
6164 } ) ;
@@ -78,22 +81,38 @@ export default function MeetingRoom() {
7881
7982 // WebSocket message handler
8083 const handleWebSocketMessage = useCallback ( ( message : WSMessage ) => {
81- console . log ( 'Received WebSocket message:' , message . type ) ;
84+ console . log ( 'Received WebSocket message:' , message . type , 'Full message:' , message ) ;
8285
8386 switch ( message . type ) {
8487 case WSMessageType . USER_JOINED :
8588 if ( message . clientId && message . clientId !== clientId ) {
86- setOtherParticipants ( ( prev ) => [ ...prev , message . clientId ! ] ) ;
87- // Create offer to new participant
88- createOffer ( message . clientId ) . then ( ( offer ) => {
89- if ( offer && sendMessageRef . current ) {
90- sendMessageRef . current ( {
91- type : WSMessageType . OFFER ,
92- target : message . clientId ,
93- data : offer ,
94- } ) ;
89+ console . log ( 'User joined:' , message . clientId ) ;
90+ setOtherParticipants ( ( prev ) => {
91+ if ( ! prev . includes ( message . clientId ! ) ) {
92+ return [ ...prev , message . clientId ! ] ;
9593 }
94+ return prev ;
9695 } ) ;
96+ // Create offer to new participant only if we don't already have a connection
97+ if ( ! hasPeerConnection ( message . clientId ) && sendMessageRef . current ) {
98+ console . log ( 'Creating offer to:' , message . clientId ) ;
99+ createOffer ( message . clientId ) . then ( ( offer ) => {
100+ if ( offer && sendMessageRef . current ) {
101+ console . log ( 'Sending offer to:' , message . clientId ) ;
102+ sendMessageRef . current ( {
103+ type : WSMessageType . OFFER ,
104+ target : message . clientId ,
105+ data : offer ,
106+ } ) ;
107+ } else {
108+ console . error ( 'Failed to create offer or sendMessageRef is null' ) ;
109+ }
110+ } ) . catch ( ( error ) => {
111+ console . error ( 'Error creating offer:' , error ) ;
112+ } ) ;
113+ } else {
114+ console . log ( 'Already have peer connection with:' , message . clientId ) ;
115+ }
97116 }
98117 break ;
99118
@@ -114,52 +133,49 @@ export default function MeetingRoom() {
114133 const others = message . participants . filter ( ( id : string ) => id !== clientId ) ;
115134 setOtherParticipants ( others ) ;
116135
117- // Create offers to existing participants when we first join
118- // Use hasPeerConnection to avoid creating duplicate offers
119- if ( ! isInitialized && others . length > 0 ) {
120- others . forEach ( ( otherId : string ) => {
121- // Only create offer if we don't already have a peer connection
122- // This prevents duplicate offers if we already received one from this peer
123- if ( ! hasPeerConnection ( otherId ) && sendMessageRef . current ) {
124- createOffer ( otherId ) . then ( ( offer ) => {
125- if ( offer && sendMessageRef . current ) {
126- sendMessageRef . current ( {
127- type : WSMessageType . OFFER ,
128- target : otherId ,
129- data : offer ,
130- } ) ;
131- }
132- } ) ;
133- }
134- } ) ;
135- }
136+ // When we first join, we receive the list of existing participants
137+ // We should wait for existing participants to send us offers (via USER_JOINED on their side)
138+ // Don't create offers here - let existing participants initiate via USER_JOINED
136139 setIsInitialized ( true ) ;
137140 }
138141 break ;
139142
140143 case WSMessageType . OFFER :
141144 if ( message . from && message . data ) {
145+ console . log ( 'Received offer from:' , message . from , 'localStream ready:' , ! ! localStream ) ;
146+ // Handle offer - the useWebRTC hook will check if local stream is ready
142147 handleOffer ( message . from , message . data ) . then ( ( answer ) => {
143148 if ( answer && sendMessageRef . current ) {
149+ console . log ( 'Sending answer to:' , message . from ) ;
144150 sendMessageRef . current ( {
145151 type : WSMessageType . ANSWER ,
146152 target : message . from ,
147153 data : answer ,
148154 } ) ;
155+ } else {
156+ console . error ( 'Failed to create answer or sendMessageRef is null' ) ;
149157 }
158+ } ) . catch ( ( error ) => {
159+ console . error ( 'Error handling offer:' , error ) ;
150160 } ) ;
151161 }
152162 break ;
153163
154164 case WSMessageType . ANSWER :
155165 if ( message . from && message . data ) {
156- handleAnswer ( message . from , message . data ) ;
166+ console . log ( 'Received answer from:' , message . from ) ;
167+ handleAnswer ( message . from , message . data ) . catch ( ( error ) => {
168+ console . error ( 'Error handling answer:' , error ) ;
169+ } ) ;
157170 }
158171 break ;
159172
160173 case WSMessageType . ICE_CANDIDATE :
161174 if ( message . from && message . data ) {
162- handleRemoteIceCandidate ( message . from , message . data ) ;
175+ console . log ( 'Received ICE candidate from:' , message . from ) ;
176+ handleRemoteIceCandidate ( message . from , message . data ) . catch ( ( error ) => {
177+ console . error ( 'Error handling ICE candidate:' , error ) ;
178+ } ) ;
163179 }
164180 break ;
165181
@@ -208,6 +224,10 @@ export default function MeetingRoom() {
208224 } ) ;
209225 }
210226 break ;
227+
228+ default :
229+ console . warn ( 'Unhandled WebSocket message type:' , message . type , message ) ;
230+ break ;
211231 }
212232 } , [ clientId , createOffer , handleOffer , handleAnswer , handleRemoteIceCandidate , removePeer , hasPeerConnection , isInitialized ] ) ;
213233
0 commit comments