‘use strict’;
console.log(‘初始化按钮’);
const callButton = document.getElementById(‘callButton’);
const hangupButton = document.getElementById(‘hangupButton’);
const sendButton = document.getElementById(‘sendButton’);
const cancleButton = document.getElementById(‘cancleButton’);
const progress = document.getElementById(‘progress’);
const progress_bar = document.getElementById(‘progress_bar’);
const pingdom = document.getElementById(‘pingdom’);
const speedDom = document.getElementById(‘speedDom’);
callButton.disabled = false;
hangupButton.disabled = true;
callButton.addEventListener(‘click’, call);
hangupButton.addEventListener(‘click’, hangup);
sendButton.addEventListener(‘click’, sendFile);
cancleButton.addEventListener(‘click’, cancleFile);
console.log(‘初始化变量’);
let startTime;
const localVideo = document.getElementById(‘localVideo’);
const remoteVideo = document.getElementById(‘remoteVideo’);
localVideo.addEventListener(‘loadedmetadata’, function() {
console.log(Local video videoWidth: ${this.videoWidth}px, videoHeight: ${this.videoHeight}px
);
});
remoteVideo.addEventListener(‘loadedmetadata’, function() {
console.log(Remote video videoWidth: ${this.videoWidth}px, videoHeight: ${this.videoHeight}px
);
});
remoteVideo.addEventListener(‘resize’, () => {
console.log(Remote video size changed to ${remoteVideo.videoWidth}x${remoteVideo.videoHeight}
);
if (startTime) {
const elapsedTime = window.performance.now() - startTime;
console.log('Setup time: ’ + elapsedTime.toFixed(3) + ‘ms’);
startTime = null;
}
});
function cancleFile(){
if (fileReader && fileReader.readyState === 1) {
console.log(‘Abort read!’);
fileReader.abort();
clearInterval(speedClock);
speed = 0;
speedDom.innerHTML = ‘’;
progress.style.display = ‘none’;
}
}
function sendFile(){
if(!pc){
alert(‘请先建立连接’);
return;
}
const file = fileSource.files[0];
if (file.size === 0) {
alert(‘File is empty, please select a non-empty file’);
return;
}
progress.style.display = ‘block’;
fileReader = new FileReader();
const chunkSize = 16354;
let offset = 0;
fileReader.addEventListener(‘error’, error => console.error(‘Error reading file:’, error));
fileReader.addEventListener(‘abort’, event => console.log(‘File reading aborted:’, event));
fileReader.addEventListener(‘load’, e => {
console.log('FileRead.onload ', e);
console.log(‘Thread ‘, _thread<thread);
// sendChannel.send(e.target.result);
if(_thread<thread){
_thread++;
var data = new Uint8Array(e.target.result.byteLength+1);
var uint8 = new Uint8Array(e.target.result);
data.set([3]);
data.set(uint8,1)
pc.send(data);
speed += e.target.result.byteLength;
offset +=