speedtest_worker.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. HTML5 Speedtest v4.4
  3. by Federico Dossena
  4. https://github.com/adolfintel/speedtest/
  5. GNU LGPLv3 License
  6. */
  7. // data reported to main thread
  8. var testStatus = 0 // 0=not started, 1=download test, 2=ping+jitter test, 3=upload test, 4=finished, 5=abort/error
  9. var dlStatus = '' // download speed in megabit/s with 2 decimal digits
  10. var ulStatus = '' // upload speed in megabit/s with 2 decimal digits
  11. var pingStatus = '' // ping in milliseconds with 2 decimal digits
  12. var jitterStatus = '' // jitter in milliseconds with 2 decimal digits
  13. var clientIp = '' // client's IP address as reported by getIP.php
  14. var log='' //telemetry log
  15. function tlog(s){log+=Date.now()+': '+s+'\n'}
  16. function twarn(s){log+=Date.now()+' WARN: '+s+'\n'; console.warn(s)}
  17. // test settings. can be overridden by sending specific values with the start command
  18. var settings = {
  19. test_order: "ID_U_P", //order in which tests will be performed as a string. D=Download, U=Upload, P=Ping+Jitter, I=IP, _=1 second delay
  20. time_ul: 15, // duration of upload test in seconds
  21. time_dl: 15, // duration of download test in seconds
  22. time_ulGraceTime: 3, //time to wait in seconds before actually measuring ul speed (wait for buffers to fill)
  23. time_dlGraceTime: 1.5, //time to wait in seconds before actually measuring dl speed (wait for TCP window to increase)
  24. count_ping: 35, // number of pings to perform in ping test
  25. url_dl: 'garbage.php', // path to a large file or garbage.php, used for download test. must be relative to this js file
  26. url_ul: 'empty.php', // path to an empty file, used for upload test. must be relative to this js file
  27. url_ping: 'empty.php', // path to an empty file, used for ping test. must be relative to this js file
  28. url_getIp: 'getIP.php', // path to getIP.php relative to this js file, or a similar thing that outputs the client's ip
  29. xhr_dlMultistream: 10, // number of download streams to use (can be different if enable_quirks is active)
  30. xhr_ulMultistream: 3, // number of upload streams to use (can be different if enable_quirks is active)
  31. xhr_ignoreErrors: 1, // 0=fail on errors, 1=attempt to restart a stream if it fails, 2=ignore all errors
  32. xhr_dlUseBlob: false, // if set to true, it reduces ram usage but uses the hard drive (useful with large garbagePhp_chunkSize and/or high xhr_dlMultistream)
  33. garbagePhp_chunkSize: 20, // size of chunks sent by garbage.php (can be different if enable_quirks is active)
  34. enable_quirks: true, // enable quirks for specific browsers. currently it overrides settings to optimize for specific browsers, unless they are already being overridden with the start command
  35. overheadCompensationFactor: 1048576/925000, //compensation for HTTP+TCP+IP+ETH overhead. 925000 is how much data is actually carried over 1048576 (1mb) bytes downloaded/uploaded. This default value assumes HTTP+TCP+IPv4+ETH with typical MTUs over the Internet. You may want to change this if you're going through your local network with a different MTU or if you're going over IPv6 (see doc.md for some other values)
  36. telemetry_level: 0, // 0=disabled, 1=basic (results only), 2=full (results+log)
  37. url_telemetry: 'telemetry.php' // path to the script that adds telemetry data to the database
  38. }
  39. var xhr = null // array of currently active xhr requests
  40. var interval = null // timer used in tests
  41. var delayTimer = null // another timer used in test
  42. var test_pointer = 0 //pointer to the next test to run inside settings.test_order
  43. /*
  44. this function is used on URLs passed in the settings to determine whether we need a ? or an & as a separator
  45. */
  46. function url_sep (url) { return url.match(/\?/) ? '&' : '?'; }
  47. /*
  48. listener for commands from main thread to this worker.
  49. commands:
  50. -status: returns the current status as a string of values spearated by a semicolon (;) in this order: testStatus;dlStatus;ulStatus;pingStatus;clientIp;jitterStatus
  51. -abort: aborts the current test
  52. -start: starts the test. optionally, settings can be passed as JSON.
  53. example: start {"time_ul":"10", "time_dl":"10", "count_ping":"50"}
  54. */
  55. this.addEventListener('message', function (e) {
  56. var params = e.data.split(' ')
  57. if (params[0] === 'status') { // return status
  58. postMessage(testStatus + ';' + dlStatus + ';' + ulStatus + ';' + pingStatus + ';' + clientIp + ';' + jitterStatus)
  59. }
  60. if (params[0] === 'start' && testStatus === 0) { // start new test
  61. testStatus = 1
  62. try {
  63. // parse settings, if present
  64. var s = {}
  65. try{
  66. var ss = e.data.substring(5)
  67. if (ss) s = JSON.parse(ss)
  68. }catch(e){ twarn('Error parsing custom settings JSON. Please check your syntax') }
  69. if (typeof s.test_order !== 'undefined') settings.test_order = s.test_order.toUpperCase() // test order
  70. if (typeof s.url_dl !== 'undefined') settings.url_dl = s.url_dl // download url
  71. if (typeof s.url_ul !== 'undefined') settings.url_ul = s.url_ul // upload url
  72. if (typeof s.url_ping !== 'undefined') settings.url_ping = s.url_ping // ping url
  73. if (typeof s.url_getIp !== 'undefined') settings.url_getIp = s.url_getIp // url to getIP.php
  74. if (typeof s.time_dl !== 'undefined') settings.time_dl = s.time_dl // duration of download test
  75. if (typeof s.time_ul !== 'undefined') settings.time_ul = s.time_ul // duration of upload test
  76. if (typeof s.enable_quirks !== 'undefined') settings.enable_quirks = s.enable_quirks // enable quirks or not
  77. // quirks for specific browsers. more may be added in future releases
  78. if (settings.enable_quirks) {
  79. var ua = navigator.userAgent
  80. if (/Firefox.(\d+\.\d+)/i.test(ua)) {
  81. // ff more precise with 1 upload stream
  82. settings.xhr_ulMultistream = 1
  83. }
  84. if (/Edge.(\d+\.\d+)/i.test(ua)) {
  85. // edge more precise with 3 download streams
  86. settings.xhr_dlMultistream = 3
  87. if (/Edge\/15.(\d+)/i.test(ua) || /Edge\/16.(\d+)/i.test(ua)) {
  88. //Edge 15 introduced a bug that causes onprogress events to not get fired, so for Edge 15, we have to use the "small chunks" workaround that reduces accuracy
  89. settings.forceIE11Workaround = true
  90. }
  91. }
  92. if (/Chrome.(\d+)/i.test(ua) && (!!self.fetch)) {
  93. // chrome more precise with 5 streams
  94. settings.xhr_dlMultistream = 5
  95. }
  96. }
  97. if (typeof s.count_ping !== 'undefined') settings.count_ping = s.count_ping // number of pings for ping test
  98. if (typeof s.xhr_dlMultistream !== 'undefined') settings.xhr_dlMultistream = s.xhr_dlMultistream // number of download streams
  99. if (typeof s.xhr_ulMultistream !== 'undefined') settings.xhr_ulMultistream = s.xhr_ulMultistream // number of upload streams
  100. if (typeof s.xhr_ignoreErrors !== 'undefined') settings.xhr_ignoreErrors = s.xhr_ignoreErrors // what to do in case of errors during the test
  101. if (typeof s.xhr_dlUseBlob !== 'undefined') settings.xhr_dlUseBlob = s.xhr_dlUseBlob // use blob for download test
  102. if (typeof s.garbagePhp_chunkSize !== 'undefined') settings.garbagePhp_chunkSize = s.garbagePhp_chunkSize // size of garbage.php chunks
  103. if (typeof s.time_dlGraceTime !== 'undefined') settings.time_dlGraceTime = s.time_dlGraceTime // dl test grace time before measuring
  104. if (typeof s.time_ulGraceTime !== 'undefined') settings.time_ulGraceTime = s.time_ulGraceTime // ul test grace time before measuring
  105. if (typeof s.overheadCompensationFactor !== 'undefined') settings.overheadCompensationFactor = s.overheadCompensationFactor //custom overhead compensation factor (default assumes HTTP+TCP+IP+ETH with typical MTUs)
  106. if (typeof s.telemetry_level !== 'undefined') settings.telemetry_level = s.telemetry_level === 'basic' ? 1 : s.telemetry_level === 'full' ? 2 : 0; // telemetry level
  107. if (typeof s.url_telemetry !== 'undefined') settings.url_telemetry = s.url_telemetry // url to telemetry.php
  108. } catch (e) { twarn('Possible error in custom test settings. Some settings may not be applied. Exception: '+e) }
  109. // run the tests
  110. tlog(JSON.stringify(settings))
  111. test_pointer=0;
  112. var runNextTest=function(){
  113. if(test_pointer>=settings.test_order.length){testStatus=4; sendTelemetry(); return;}
  114. switch(settings.test_order.charAt(test_pointer)){
  115. case 'I':{test_pointer++; getIp(runNextTest);} break;
  116. case 'D':{test_pointer++; testStatus=1; dlTest(runNextTest);} break;
  117. case 'U':{test_pointer++; testStatus=3; ulTest(runNextTest);} break;
  118. case 'P':{test_pointer++; testStatus=2; pingTest(runNextTest);} break;
  119. case '_':{test_pointer++; delayTimer=setTimeout(runNextTest,1000);} break;
  120. default: test_pointer++;
  121. }
  122. }
  123. runNextTest()
  124. }
  125. if (params[0] === 'abort') { // abort command
  126. tlog('manually aborted')
  127. clearRequests() // stop all xhr activity
  128. runNextTest=null;
  129. if (interval) clearInterval(interval) // clear timer if present
  130. if (settings.telemetry_level > 1) sendTelemetry()
  131. testStatus = 5; dlStatus = ''; ulStatus = ''; pingStatus = ''; jitterStatus = '' // set test as aborted
  132. }
  133. })
  134. // stops all XHR activity, aggressively
  135. function clearRequests () {
  136. tlog('stopping pending XHRs')
  137. if (xhr) {
  138. for (var i = 0; i < xhr.length; i++) {
  139. try { xhr[i].onprogress = null; xhr[i].onload = null; xhr[i].onerror = null } catch (e) { }
  140. try { xhr[i].upload.onprogress = null; xhr[i].upload.onload = null; xhr[i].upload.onerror = null } catch (e) { }
  141. try { xhr[i].abort() } catch (e) { }
  142. try { delete (xhr[i]) } catch (e) { }
  143. }
  144. xhr = null
  145. }
  146. }
  147. // gets client's IP using url_getIp, then calls the done function
  148. var ipCalled = false // used to prevent multiple accidental calls to getIp
  149. function getIp (done) {
  150. tlog('getIp')
  151. if (ipCalled) return; else ipCalled = true // getIp already called?
  152. xhr = new XMLHttpRequest()
  153. xhr.onload = function () {
  154. tlog("IP: "+xhr.responseText)
  155. clientIp = xhr.responseText
  156. done()
  157. }
  158. xhr.onerror = function () {
  159. tlog('getIp failed')
  160. done()
  161. }
  162. xhr.open('GET', settings.url_getIp + url_sep(settings.url_getIp) + 'r=' + Math.random(), true)
  163. xhr.send()
  164. }
  165. // download test, calls done function when it's over
  166. var dlCalled = false // used to prevent multiple accidental calls to dlTest
  167. function dlTest (done) {
  168. tlog('dlTest')
  169. if (dlCalled) return; else dlCalled = true // dlTest already called?
  170. var totLoaded = 0.0, // total number of loaded bytes
  171. startT = new Date().getTime(), // timestamp when test was started
  172. graceTimeDone = false, //set to true after the grace time is past
  173. failed = false // set to true if a stream fails
  174. xhr = []
  175. // function to create a download stream. streams are slightly delayed so that they will not end at the same time
  176. var testStream = function (i, delay) {
  177. setTimeout(function () {
  178. if (testStatus !== 1) return // delayed stream ended up starting after the end of the download test
  179. tlog('dl test stream started '+i+' '+delay)
  180. var prevLoaded = 0 // number of bytes loaded last time onprogress was called
  181. var x = new XMLHttpRequest()
  182. xhr[i] = x
  183. xhr[i].onprogress = function (event) {
  184. tlog('dl stream progress event '+i+' '+event.loaded)
  185. if (testStatus !== 1) { try { x.abort() } catch (e) { } } // just in case this XHR is still running after the download test
  186. // progress event, add number of new loaded bytes to totLoaded
  187. var loadDiff = event.loaded <= 0 ? 0 : (event.loaded - prevLoaded)
  188. if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return // just in case
  189. totLoaded += loadDiff
  190. prevLoaded = event.loaded
  191. }.bind(this)
  192. xhr[i].onload = function () {
  193. // the large file has been loaded entirely, start again
  194. tlog('dl stream finished '+i)
  195. try { xhr[i].abort() } catch (e) { } // reset the stream data to empty ram
  196. testStream(i, 0)
  197. }.bind(this)
  198. xhr[i].onerror = function () {
  199. // error
  200. tlog('dl stream failed '+i)
  201. if (settings.xhr_ignoreErrors === 0) failed=true //abort
  202. try { xhr[i].abort() } catch (e) { }
  203. delete (xhr[i])
  204. if (settings.xhr_ignoreErrors === 1) testStream(i, 100) //restart stream after 100ms
  205. }.bind(this)
  206. // send xhr
  207. try { if (settings.xhr_dlUseBlob) xhr[i].responseType = 'blob'; else xhr[i].responseType = 'arraybuffer' } catch (e) { }
  208. xhr[i].open('GET', settings.url_dl + url_sep(settings.url_dl) + 'r=' + Math.random() + '&ckSize=' + settings.garbagePhp_chunkSize, true) // random string to prevent caching
  209. xhr[i].send()
  210. }.bind(this), 1 + delay)
  211. }.bind(this)
  212. // open streams
  213. for (var i = 0; i < settings.xhr_dlMultistream; i++) {
  214. testStream(i, 100 * i)
  215. }
  216. // every 200ms, update dlStatus
  217. interval = setInterval(function () {
  218. tlog('DL: '+dlStatus+(graceTimeDone?'':' (in grace time)'))
  219. var t = new Date().getTime() - startT
  220. if (t < 200) return
  221. if (!graceTimeDone){
  222. if (t > 1000 * settings.time_dlGraceTime){
  223. if (totLoaded > 0){ // if the connection is so slow that we didn't get a single chunk yet, do not reset
  224. startT = new Date().getTime()
  225. totLoaded = 0.0;
  226. }
  227. graceTimeDone = true;
  228. }
  229. }else{
  230. var speed = totLoaded / (t / 1000.0)
  231. dlStatus = ((speed * 8 * settings.overheadCompensationFactor)/1048576).toFixed(2) // speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 to go to megabits/s
  232. if (((t / 1000.0) > settings.time_dl && dlStatus > 0) || failed) { // test is over, stop streams and timer
  233. if (failed || isNaN(dlStatus)) dlStatus = 'Fail'
  234. clearRequests()
  235. clearInterval(interval)
  236. tlog('dlTest finished '+dlStatus)
  237. done()
  238. }
  239. }
  240. }.bind(this), 200)
  241. }
  242. // upload test, calls done function whent it's over
  243. // garbage data for upload test
  244. var r = new ArrayBuffer(1048576)
  245. try { r = new Float32Array(r); for (var i = 0; i < r.length; i++)r[i] = Math.random() } catch (e) { }
  246. var req = []
  247. var reqsmall = []
  248. for (var i = 0; i < 20; i++) req.push(r)
  249. req = new Blob(req)
  250. r = new ArrayBuffer(262144)
  251. try { r = new Float32Array(r); for (var i = 0; i < r.length; i++)r[i] = Math.random() } catch (e) { }
  252. reqsmall.push(r)
  253. reqsmall = new Blob(reqsmall)
  254. var ulCalled = false // used to prevent multiple accidental calls to ulTest
  255. function ulTest (done) {
  256. tlog('ulTest')
  257. if (ulCalled) return; else ulCalled = true // ulTest already called?
  258. var totLoaded = 0.0, // total number of transmitted bytes
  259. startT = new Date().getTime(), // timestamp when test was started
  260. graceTimeDone = false, //set to true after the grace time is past
  261. failed = false // set to true if a stream fails
  262. xhr = []
  263. // function to create an upload stream. streams are slightly delayed so that they will not end at the same time
  264. var testStream = function (i, delay) {
  265. setTimeout(function () {
  266. if (testStatus !== 3) return // delayed stream ended up starting after the end of the upload test
  267. tlog('ul test stream started '+i+' '+delay)
  268. var prevLoaded = 0 // number of bytes transmitted last time onprogress was called
  269. var x = new XMLHttpRequest()
  270. xhr[i] = x
  271. var ie11workaround
  272. if (settings.forceIE11Workaround) ie11workaround = true; else {
  273. try {
  274. xhr[i].upload.onprogress
  275. ie11workaround = false
  276. } catch (e) {
  277. ie11workaround = true
  278. }
  279. }
  280. if (ie11workaround) {
  281. // IE11 workarond: xhr.upload does not work properly, therefore we send a bunch of small 256k requests and use the onload event as progress. This is not precise, especially on fast connections
  282. xhr[i].onload = function () {
  283. tlog('ul stream progress event (ie11wa)')
  284. totLoaded += 262144
  285. testStream(i, 0)
  286. }
  287. xhr[i].onerror = function () {
  288. // error, abort
  289. tlog('ul stream failed (ie11wa)')
  290. if (settings.xhr_ignoreErrors === 0) failed = true //abort
  291. try { xhr[i].abort() } catch (e) { }
  292. delete (xhr[i])
  293. if (settings.xhr_ignoreErrors === 1) testStream(i,100); //restart stream after 100ms
  294. }
  295. xhr[i].open('POST', settings.url_ul + url_sep(settings.url_ul) + 'r=' + Math.random(), true) // random string to prevent caching
  296. xhr[i].setRequestHeader('Content-Encoding', 'identity') // disable compression (some browsers may refuse it, but data is incompressible anyway)
  297. xhr[i].send(reqsmall)
  298. } else {
  299. // REGULAR version, no workaround
  300. xhr[i].upload.onprogress = function (event) {
  301. tlog('ul stream progress event '+i+' '+event.loaded)
  302. if (testStatus !== 3) { try { x.abort() } catch (e) { } } // just in case this XHR is still running after the upload test
  303. // progress event, add number of new loaded bytes to totLoaded
  304. var loadDiff = event.loaded <= 0 ? 0 : (event.loaded - prevLoaded)
  305. if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return // just in case
  306. totLoaded += loadDiff
  307. prevLoaded = event.loaded
  308. }.bind(this)
  309. xhr[i].upload.onload = function () {
  310. // this stream sent all the garbage data, start again
  311. tlog('ul stream finished '+i)
  312. testStream(i, 0)
  313. }.bind(this)
  314. xhr[i].upload.onerror = function () {
  315. tlog('ul stream failed '+i)
  316. if (settings.xhr_ignoreErrors === 0) failed=true //abort
  317. try { xhr[i].abort() } catch (e) { }
  318. delete (xhr[i])
  319. if (settings.xhr_ignoreErrors === 1) testStream(i, 100) //restart stream after 100ms
  320. }.bind(this)
  321. // send xhr
  322. xhr[i].open('POST', settings.url_ul + url_sep(settings.url_ul) + 'r=' + Math.random(), true) // random string to prevent caching
  323. xhr[i].setRequestHeader('Content-Encoding', 'identity') // disable compression (some browsers may refuse it, but data is incompressible anyway)
  324. xhr[i].send(req)
  325. }
  326. }.bind(this), 1)
  327. }.bind(this)
  328. // open streams
  329. for (var i = 0; i < settings.xhr_ulMultistream; i++) {
  330. testStream(i, 100 * i)
  331. }
  332. // every 200ms, update ulStatus
  333. interval = setInterval(function () {
  334. tlog('UL: '+ulStatus+(graceTimeDone?'':' (in grace time)'))
  335. var t = new Date().getTime() - startT
  336. if (t < 200) return
  337. if (!graceTimeDone){
  338. if (t > 1000 * settings.time_ulGraceTime){
  339. if (totLoaded > 0){ // if the connection is so slow that we didn't get a single chunk yet, do not reset
  340. startT = new Date().getTime()
  341. totLoaded = 0.0;
  342. }
  343. graceTimeDone = true;
  344. }
  345. }else{
  346. var speed = totLoaded / (t / 1000.0)
  347. ulStatus = ((speed * 8 * settings.overheadCompensationFactor)/1048576).toFixed(2) // speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 to go to megabits/s
  348. if (((t / 1000.0) > settings.time_ul && ulStatus > 0) || failed) { // test is over, stop streams and timer
  349. if (failed || isNaN(ulStatus)) ulStatus = 'Fail'
  350. clearRequests()
  351. clearInterval(interval)
  352. tlog('ulTest finished '+ulStatus)
  353. done()
  354. }
  355. }
  356. }.bind(this), 200)
  357. }
  358. // ping+jitter test, function done is called when it's over
  359. var ptCalled = false // used to prevent multiple accidental calls to pingTest
  360. function pingTest (done) {
  361. tlog('pingTest')
  362. if (ptCalled) return; else ptCalled = true // pingTest already called?
  363. var prevT = null // last time a pong was received
  364. var ping = 0.0 // current ping value
  365. var jitter = 0.0 // current jitter value
  366. var i = 0 // counter of pongs received
  367. var prevInstspd = 0 // last ping time, used for jitter calculation
  368. xhr = []
  369. // ping function
  370. var doPing = function () {
  371. tlog('ping')
  372. prevT = new Date().getTime()
  373. xhr[0] = new XMLHttpRequest()
  374. xhr[0].onload = function () {
  375. // pong
  376. tlog('pong')
  377. if (i === 0) {
  378. prevT = new Date().getTime() // first pong
  379. } else {
  380. var instspd = (new Date().getTime() - prevT)
  381. var instjitter = Math.abs(instspd - prevInstspd)
  382. if (i === 1) ping = instspd; /* first ping, can't tell jitter yet*/ else {
  383. ping = ping * 0.9 + instspd * 0.1 // ping, weighted average
  384. jitter = instjitter > jitter ? (jitter * 0.2 + instjitter * 0.8) : (jitter * 0.9 + instjitter * 0.1) // update jitter, weighted average. spikes in ping values are given more weight.
  385. }
  386. prevInstspd = instspd
  387. }
  388. pingStatus = ping.toFixed(2)
  389. jitterStatus = jitter.toFixed(2)
  390. i++
  391. tlog('PING: '+pingStatus+' JITTER: '+jitterStatus)
  392. if (i < settings.count_ping) doPing(); else done() // more pings to do?
  393. }.bind(this)
  394. xhr[0].onerror = function () {
  395. // a ping failed, cancel test
  396. tlog('ping failed')
  397. if (settings.xhr_ignoreErrors === 0) { //abort
  398. pingStatus = 'Fail'
  399. jitterStatus = 'Fail'
  400. clearRequests()
  401. done()
  402. }
  403. if (settings.xhr_ignoreErrors === 1) doPing() //retry ping
  404. if (settings.xhr_ignoreErrors === 2){ //ignore failed ping
  405. i++
  406. if (i < settings.count_ping) doPing(); else done() // more pings to do?
  407. }
  408. }.bind(this)
  409. // sent xhr
  410. xhr[0].open('GET', settings.url_ping + url_sep(settings.url_ping) + 'r=' + Math.random(), true) // random string to prevent caching
  411. xhr[0].send()
  412. }.bind(this)
  413. doPing() // start first ping
  414. }
  415. // telemetry
  416. function sendTelemetry(){
  417. if (settings.telemetry_level < 1) return
  418. xhr = new XMLHttpRequest()
  419. xhr.onload = function () { console.log('TELEMETRY OL '+xhr.responseText) }
  420. xhr.onerror = function () { console.log('TELEMETRY ERROR '+xhr) }
  421. xhr.open('POST', settings.url_telemetry+"?r="+Math.random(), true);
  422. try{
  423. var fd = new FormData()
  424. fd.append('dl', dlStatus)
  425. fd.append('ul', ulStatus)
  426. fd.append('ping', pingStatus)
  427. fd.append('jitter', jitterStatus)
  428. fd.append('log', settings.telemetry_level>1?log:"")
  429. xhr.send(fd)
  430. }catch(ex){
  431. var postData = 'dl='+encodeURIComponent(dlStatus)+'&ul='+encodeURIComponent(ulStatus)+'&ping='+encodeURIComponent(pingStatus)+'&jitter='+encodeURIComponent(jitterStatus)+'&log='+encodeURIComponent(settings.telemetry_level>1?log:'')
  432. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  433. xhr.send(postData)
  434. }
  435. }