@@ -15,9 +15,30 @@ const io = require("socket.io")(server, {
15
15
io . on ( "connection" , ( socket ) => {
16
16
// emit endCall to the room it was in.
17
17
socket . on ( "disconnecting" , ( ) => {
18
- socket . to ( Array . from ( socket . rooms ) )
19
- . except ( socket . id )
20
- . emit ( "endCall" ) ;
18
+ // for each room in the disconnecting socket...
19
+ socket . rooms . forEach ( ( target ) => {
20
+ // ignoring the room matching its own id...
21
+ if ( target === socket . id ) {
22
+ return ;
23
+ }
24
+ // get the user ids within the room...
25
+ io . sockets . adapter . rooms
26
+ . get ( target )
27
+ . forEach (
28
+ ( id ) => {
29
+ // and for each user id in the room not matching
30
+ // its own id...
31
+ if ( id === socket . id ) {
32
+ return ;
33
+ }
34
+ // leave the target room...
35
+ io . sockets . sockets . get ( id ) . leave ( target ) ;
36
+ console . log ( id + " leaves " + target ) ;
37
+ // then tell the user id to endCall.
38
+ io . to ( id ) . emit ( "endCall" ) ;
39
+ }
40
+ ) ;
41
+ } ) ;
21
42
} ) ;
22
43
23
44
// join a room and inform the peer that the other person has joined
@@ -27,11 +48,6 @@ io.on("connection", (socket) => {
27
48
socket . to ( data . target ) . emit ( "peerConnected" ) ;
28
49
} ) ;
29
50
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
-
35
51
// propagate the socket events for starting and handshaking a call forward.
36
52
socket . on ( "startCall" , ( data ) => {
37
53
console . log ( socket . id + " is starting call in " + data . target ) ;
0 commit comments