Hanging up a call

Our last bit is not part of the WebRTC specification, but is still a good feature to have—hanging up. This will allow our users to disconnect from another user so they are available to call someone else. This will also notify our server to disconnect any user references we have in our code. You can add the ""leave"" handler as detailed in the following code:

case ""leave"":
        console.log(""Disconnecting user from"", data.name);
        var conn = users[data.name];
        conn.otherName = null;

        if (conn != null) {
          sendTo(conn, {
            type: ""leave""
          });
        }

        break;

This will also notify the other user of the leave event so they can disconnect their peer connection accordingly. Another thing we have to do is handle the case of when a user drops their connection from the signaling server. This means we can no longer serve them and that we need to terminate their calls. We can change the close handler we used before to look similar to this:

connection.on(''close'', function () {
    if (connection.name) {
      delete users[connection.name];

      if (connection.otherName) {
        console.log(""Disconnecting user from"", connection.otherName);
        var conn = users[connection.otherName];
        conn.otherName = null;

        if (conn != null) {
          sendTo(conn, {
            type: ""leave""
          });
        }
      }
    }
  });

This will now disconnect our users if they happen to terminate their connection unexpectedly from the server. This can help in cases where we are still in offer, answer, or a candidate state but the other user closes their browser window. In this case, the WebRTC API will not send any events of this happening and we need another way to know that the user has left. Having the signaling server handle in this case helps make our application more reliable and stable overall.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.254.94