reactjsreact-nativewebrtcpeerjsreact-native-webrtc

How to end peer connection in react-native


how can i end peer connection when using react-native-peerjs..The connection is successful& the call works. but at the point of ending the call, i haven't found a disconnect function, so the call still goes on in background till i restart the app.

my connection is like this

 const peerServer = new Peer(undefined, {
    host: 'mvmarketserver.herokuapp.com',
    config: {
        iceServers: [
            {url:'stun:eu-turn3.xirsys.com'},
            {
                username: "",
                credential: "",
                credentialType: 'password',
                urls: [
                    "turn:eu-turn3.xirsys.com:80?transport=udp",
                    "turn:eu-turn3.xirsys.com:3478?transport=udp",
                    "turn:eu-turn3.xirsys.com:80?transport=tcp",
                    "turn:eu-turn3.xirsys.com:3478?transport=tcp",
                    "turns:eu-turn3.xirsys.com:443?transport=tcp",
                    "turns:eu-turn3.xirsys.com:5349?transport=tcp",
                ]
            }
        ]
    },
    secure: true,
    port: 443,
    path: '/mypeer'
})

peerServer.on('error', console.log)

Currently my disconnect is like this, i try to use new Peer() to see if creating a new one, ends the previous one.Please help

dispatch({type: DELETE_STREAM, payload: []})
    dispatch({type: DELETE_MY_STREAM, payload: null})
    dispatch({type: DELETE_REMOTE_STREAM, payload: []})
KeepAwake.deactivate()
            SoundPlayer.stop()
            const roomID = this.state.room;
            console.log('leave ' + roomID);
            socket.emit('end-call', {roomID}); 
            const peer = new Peer();
            console.log('disconnected')
            InCallManager.stop({ busytone: '_DTMF_'})
            this.props.leaveRoom()
            const {goBack} = this.props.navigation;
            goBack(null);

Solution

  • Here I use native peerConnection but maybe you can try this way.

    peerServer.on('connection', conn => {
        conn.on('close', () => {
            console.log("Close PeerConnection");
            conn.close();
        });
    })
    

    But if this does't work, maybe conn.on('close') not trigered. https://github.com/peers/peerjs/issues/636

    if you are using SocketIo you can add suck event as

    socket.broadcast.emit("user-disconnected", {SOME_DATA_WHAT_YOU_WANT_TO_SEND}); 
    

    and you can on event in client

    socket.on("user-disconnected", (userId)=>{
        // Close peer connection or remove all video or audio stream
        closePeer()
    });
    

    create closePeer function

    function closePeer() {
        for (let conns in peerServer.connections) {
          peerServer.connections[conns].forEach((conn, index, array) => {
            conn.peerConnection.close();
            if (conn.close)
              conn.close();
          });
        }
      }