Book Image

Learning WebRTC

By : Daniel M. Ristic
Book Image

Learning WebRTC

By: Daniel M. Ristic

Overview of this book

Table of Contents (16 chapters)
Learning WebRTC
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Hanging up a call


The last feature we will implement is the ability to hang up an in-progress call. This will notify the other user of our intention to close the call and stop transmitting information. It will take just a few additional lines to our JavaScript:

hangUpButton.addEventListener("click", function () {
  send({
    type: "leave"
  });

  onLeave();
});

function onLeave() {
  connectedUser = null;
  theirVideo.src = null;
  yourConnection.close();
  yourConnection.onicecandidate = null;
  yourConnection.onaddstream = null;
  setupPeerConnection(stream);
};

When the user clicks on the Hang Up button, it will send a message to the other user and destroy the connection locally. There are a few things required to successfully destroy the connection and also allow another call to be made in the future:

  1. First off, we need to notify our server that we are no longer communicating.

  2. Secondly, we need to tell RTCPeerConnection to close, and this will stop transmitting our stream data to the...