Skip to content

Commit b13d1b2

Browse files
committed
Add refresh of simple-peer objects when DC
1 parent 37e1bcb commit b13d1b2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

comms/server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ io.on("connection", (socket) => {
2727
socket.to(data.target).emit("peerConnected");
2828
});
2929

30+
// leave the room - this is performed as part of a cleanup function.
31+
socket.on("leaveRoom", (data) => {
32+
socket.leave(data.target);
33+
});
34+
3035
// propagate the socket events for starting and handshaking a call forward.
3136
socket.on("startCall", (data) => {
3237
console.log(socket.id + " is starting call in "+ data.target);

peerprep/components/questionpage/CommsPanel.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ function CommsPanel({ className, roomId }: Props) {
8787
attachSocketReceiver(stream, roomId, userVideo, connectionRef);
8888

8989
socket.on("endCall", () => {
90+
// immediately destroy the socket listeners
91+
destroyCallListeners(roomId);
9092
if (userVideo.current) {
9193
(userVideo.current.srcObject as MediaStream)
9294
.getTracks()
@@ -95,6 +97,16 @@ function CommsPanel({ className, roomId }: Props) {
9597
});
9698
userVideo.current.srcObject = null;
9799
}
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+
});
98110
});
99111

100112
socket.emit("joinRoom", {
@@ -126,6 +138,15 @@ function CommsPanel({ className, roomId }: Props) {
126138
);
127139
}
128140

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+
129150
function attachSocketReceiver(
130151
stream: MediaStream,
131152
roomId: string,

0 commit comments

Comments
 (0)