-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdetectbandwidth.js
More file actions
61 lines (52 loc) · 1.67 KB
/
detectbandwidth.js
File metadata and controls
61 lines (52 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"use strict";
self.addEventListener('message', function (e) {
var data, url, start, fileSize;
data = e.data;
url = data.testFile;
start=new Date();
start=start.getTime();
getFileSize();
analyze();
function getFileSize() {
try {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', url, false);
//xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
fileSize = xhr.getResponseHeader('Content-Length');
}
}
};
xhr.send(null);
} catch (e) {
}
}
function analyze() {
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
result();
}
}
};
xhr.send(null);
} catch (e) {
postMessage(null);
}
}
function result(v) {
var end, rate, duration, message;
end = new Date();
end = end.getTime();
duration = end-start;
rate = Math.round( (fileSize/(end-start)) *10 ) /10;
message = '{"fileSize":' + fileSize + ',"rate":'+ rate + ',"duration":' + duration + '}';
postMessage(message);
}
}, false);