@@ -87,6 +87,8 @@ function CommsPanel({ className, roomId }: Props) {
87
87
attachSocketReceiver ( stream , roomId , userVideo , connectionRef ) ;
88
88
89
89
socket . on ( "endCall" , ( ) => {
90
+ // immediately destroy the socket listeners
91
+ destroyCallListeners ( roomId ) ;
90
92
if ( userVideo . current ) {
91
93
( userVideo . current . srcObject as MediaStream )
92
94
. getTracks ( )
@@ -95,6 +97,16 @@ function CommsPanel({ className, roomId }: Props) {
95
97
} ) ;
96
98
userVideo . current . srcObject = null ;
97
99
}
100
+ if ( connectionRef . current && ! connectionRef . current . destroyed ) {
101
+ connectionRef . current . destroy ( ) ;
102
+ }
103
+ // reattach the sockets
104
+ attachSocketInitiator ( stream , roomId , userVideo , connectionRef ) ;
105
+ attachSocketReceiver ( stream , roomId , userVideo , connectionRef ) ;
106
+ // rejoin the room
107
+ socket . emit ( "joinRoom" , {
108
+ target : roomId ,
109
+ } ) ;
98
110
} ) ;
99
111
100
112
socket . emit ( "joinRoom" , {
@@ -126,6 +138,15 @@ function CommsPanel({ className, roomId }: Props) {
126
138
) ;
127
139
}
128
140
141
+ function destroyCallListeners ( roomId : string ) {
142
+ socket . emit ( "leaveRoom" , {
143
+ target : roomId ,
144
+ } ) ;
145
+ socket . removeAllListeners ( "startCall" ) ;
146
+ socket . removeAllListeners ( "peerConnected" ) ;
147
+ socket . removeAllListeners ( "handshakeCall" ) ;
148
+ }
149
+
129
150
function attachSocketReceiver (
130
151
stream : MediaStream ,
131
152
roomId : string ,
0 commit comments