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

Putting it together on the other side


Now the other user should have a collection of chunks that make up the entire file. Since we used the ordered option in the data channel, the chunks should have come from the other user in order. This means we can put them together in one large data object as we receive them from the other side.

To store our pieces of data, we can push them into a simple array data structure in the client. We will do this directly when the data channel reports that a message has been received. We will also use the opposite decoding function that we covered in the Making chunks readable subsection under the Breaking down a file into chunks section in this chapter, to put our chunks into the right format:

// Add some new global variables
var currentFile = [],
    currentFileMeta;

// Add this to the openDataChannel function
dataChannel.onmessage = function (event) {
    try {
      var message = JSON.parse(event.data);

      switch (message.type) {
        case "start"...