From here on, our code does not get any more complex than the login
handler. We will create a set of handlers to pass our message correctly for each step of the way. One of the first calls that is made after logging in is the offer
handler, which designates that one user would like to call another.
It is a good idea not to get call initiations mixed up with the offer
step of WebRTC. In this example, we have combined the two to make our API easier to work with. In most settings, these steps will be separated. This can be seen in an application, such as Skype, where the other user has to accept the incoming call before a connection is established between the two users.
We can now add the offer
handler into this code:
case ""offer"": console.log(""Sending offer to"", data.name); var conn = users[data.name]; if (conn != null) { connection.otherName = data.name; sendTo(conn, { type: ""offer"", offer: data.offer, name: connection.name }); } break;
The first thing we do is get connection
of the user we are trying to call. This is easy to do since the ID of the other user is always where our connection
is stored in our user-lookup object. We then check if the other user exists and if so, send them the details of offer
. We also add an otherName
property to the user's connection
object so that we can look this up easily later on in the code. You might also notice that none of this code is WebRTC-specific. This could potentially refer to any sort of calling technology between two users. We will cover this in more detail later on in the chapter.
Something you may also notice is the lack of error handling. This is perhaps one of the most tedious parts of WebRTC. Since a call can fail at any point of the process, we have many places where making a connection can fail. It can also fail for various reasons, such as network availability, firewalls, and more. In this book, we leave it up to the user to handle each error case individually in the manner that they would like.
18.223.32.230