Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
Merged blow-away tweaks to data channel file transfer demo
  • Loading branch information
samdutton committed Feb 28, 2015
2 parents 9acc5ba + f308ec1 commit 9937adb
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/content/datachannel/filetransfer/js/main.js
Expand Up @@ -15,7 +15,7 @@ var receiveChannel;
var pcConstraint;
var bitrateDiv = document.querySelector('div#bitrate');
var fileInput = document.querySelector('input#fileInput');
fileInput.addEventListener('change', createConnection, false);
var downloadDiv = document.querySelector('a#received');
var sendProgress = document.querySelector('progress#sendProgress');
var receiveProgress = document.querySelector('progress#receiveProgress');

Expand All @@ -28,6 +28,8 @@ var timestampStart;
var statsInterval = null;
var bitrateMax = 0;

fileInput.addEventListener('change', createConnection, false);

function createConnection() {
var servers = null;
pcConstraint = null;
Expand Down Expand Up @@ -148,10 +150,19 @@ function receiveChannelCallback(event) {
receiveChannel.onmessage = onReceiveMessageCallback;
receiveChannel.onopen = onReceiveChannelStateChange;
receiveChannel.onclose = onReceiveChannelStateChange;

receivedSize = 0;
bitrateMax = 0;
downloadDiv.innerHTML = '';
downloadDiv.removeAttribute('download');
if (downloadDiv.href) {
URL.revokeObjectURL(downloadDiv.href);
downloadDiv.removeAttribute('href');
}
}

function onReceiveMessageCallback(event) {
trace('Received Message ' + event.data.byteLength);
//trace('Received Message ' + event.data.byteLength);
receiveBuffer.push(event.data);
receivedSize += event.data.byteLength;

Expand All @@ -164,13 +175,12 @@ function onReceiveMessageCallback(event) {
var received = new window.Blob(receiveBuffer);
receiveBuffer = [];

var href = document.getElementById('received');
href.href = URL.createObjectURL(received);
href.download = file.name;
downloadDiv.href = URL.createObjectURL(received);
downloadDiv.download = file.name;
var text = 'Click to download \'' + file.name + '\' (' + file.size +
' bytes)';
href.appendChild(document.createTextNode(text));
href.style.display = 'block';
downloadDiv.appendChild(document.createTextNode(text));
downloadDiv.style.display = 'block';

var bitrate = Math.round(receivedSize * 8 /
((new Date()).getTime() - timestampStart));
Expand All @@ -183,6 +193,9 @@ function onReceiveMessageCallback(event) {
}

closeDataChannels();

// re-enable the file select
fileInput.disabled = false;
}
}

Expand Down

0 comments on commit 9937adb

Please sign in to comment.