diff --git a/ui/src/views/classroom/RoomDetail.tsx b/ui/src/views/classroom/RoomDetail.tsx index 067f6a5a..e11c6849 100644 --- a/ui/src/views/classroom/RoomDetail.tsx +++ b/ui/src/views/classroom/RoomDetail.tsx @@ -136,6 +136,8 @@ const RoomDetail: React.FC = () => { autoMuteNewParticipants: true, }) + const hasTeacher = (list: ClassroomParticipantDto[]) => list.some((p) => p.isTeacher) + const signalRServiceRef = useRef() const webRTCServiceRef = useRef() @@ -270,35 +272,9 @@ const RoomDetail: React.FC = () => { console.log(`Participant joined: ${name}, isTeacher: ${isTeacher}`) - if (webRTCServiceRef.current) { - if (!webRTCServiceRef.current.getPeerConnection(userId)) { - await webRTCServiceRef.current.createPeerConnection(userId) - } - - // student → teacher - if (user.role === 'student' && isTeacher) { - const offer = await webRTCServiceRef.current.createOffer(userId) - await signalRServiceRef.current?.sendOffer(classSession.id, userId, offer) - } - - // teacher → student - if (user.role === 'teacher' && !isTeacher) { - const offer = await webRTCServiceRef.current.createOffer(userId) - await signalRServiceRef.current?.sendOffer(classSession.id, userId, offer) - } - - // teacher ↔ teacher - if (user.role === 'teacher' && isTeacher) { - if (user.id < userId) { - const offer = await webRTCServiceRef.current.createOffer(userId) - await signalRServiceRef.current?.sendOffer(classSession.id, userId, offer) - } - } - } - + // Katılımcıyı state'e ekle setParticipants((prev) => { - const exists = prev.find((p) => p.id === userId) - if (exists) return prev + if (prev.find((p) => p.id === userId)) return prev return [ ...prev, { @@ -310,6 +286,23 @@ const RoomDetail: React.FC = () => { }, ] }) + + // ✅ sadece öğretmen sınıfta ise offer başlasın + const teacherExists = hasTeacher(participants) || isTeacher + if (!teacherExists) { + console.log('Teacher yok, yeni join için offer başlatılmıyor.') + return + } + + if (!webRTCServiceRef.current?.getPeerConnection(userId)) { + await webRTCServiceRef.current?.createPeerConnection(userId) + } + + // Küçük id kuralı + if (user.id < userId) { + const offer = await webRTCServiceRef.current!.createOffer(userId) + await signalRServiceRef.current?.sendOffer(classSession.id, userId, offer) + } }, ) @@ -319,52 +312,9 @@ const RoomDetail: React.FC = () => { for (const participant of existing) { if (participant.userId === user.id) continue - console.log( - `Existing participant: ${participant.userName}, isTeacher: ${participant.isTeacher}`, - ) - - if (webRTCServiceRef.current) { - if (!webRTCServiceRef.current.getPeerConnection(participant.userId)) { - await webRTCServiceRef.current.createPeerConnection(participant.userId) - } - - // 🔑 Eğer ben öğrenci isem, öğretmen için offer gönder - if (user.role === 'student' && participant.isTeacher) { - const offer = await webRTCServiceRef.current.createOffer(participant.userId) - await signalRServiceRef.current?.sendOffer( - classSession.id, - participant.userId, - offer, - ) - } - - // 🔑 Eğer ben öğretmensem, öğrenci için offer gönder - if (user.role === 'teacher' && !participant.isTeacher) { - const offer = await webRTCServiceRef.current.createOffer(participant.userId) - await signalRServiceRef.current?.sendOffer( - classSession.id, - participant.userId, - offer, - ) - } - - // 🔑 Öğretmen ↔ Öğretmen - if (user.role === 'teacher' && participant.isTeacher) { - // id’si küçük olan offer göndersin - if (user.id < participant.userId) { - const offer = await webRTCServiceRef.current.createOffer(participant.userId) - await signalRServiceRef.current?.sendOffer( - classSession.id, - participant.userId, - offer, - ) - } - } - } - + // Katılımcıyı state'e ekle setParticipants((prev) => { - const exists = prev.find((p) => p.id === participant.userId) - if (exists) return prev + if (prev.find((p) => p.id === participant.userId)) return prev return [ ...prev, { @@ -377,6 +327,27 @@ const RoomDetail: React.FC = () => { ] }) } + + // ✅ sadece öğretmen varsa offer başlat + const teacherExists = existing.some((p) => p.isTeacher) + if (!teacherExists) { + console.log('Teacher yok, offer başlatılmıyor.') + return + } + + for (const participant of existing) { + if (participant.userId === user.id) continue + + if (!webRTCServiceRef.current?.getPeerConnection(participant.userId)) { + await webRTCServiceRef.current?.createPeerConnection(participant.userId) + } + + // Küçük id'li offer göndersin (çift offer olmaması için) + if (user.id < participant.userId) { + const offer = await webRTCServiceRef.current!.createOffer(participant.userId) + await signalRServiceRef.current?.sendOffer(classSession.id, participant.userId, offer) + } + } }, )