如何解决当使用XMLHttpRequest
我正在使用Electron创建桌面软件。我从我的VPS向Binance API发送了一个请求,要求它进行320毫秒的ping操作。但是当我在Electron的开发人员环境中发送请求时,Ping最多翻了700毫秒。谁能帮我解决问题?我不知道问题出在Electron或Nodejs的一面,还是由于Electron的开发人员环境,在软件发布后一切都会好起来吗?为了计算ping,我使用了以下方法:
// defining a timer
class Timer {
constructor () {
this.isRunning = false;
this.startTime = 0;
this.overallTime = 0;
}
_getTimeElapsedSinceLastStart () {
if (!this.startTime) {
return 0;
}
return Date.now() - this.startTime;
}
start () {
if (this.isRunning) {
return console.error('Timer is already running');
}
this.isRunning = true;
this.startTime = Date.now();
}
stop () {
if (!this.isRunning) {
return console.error('Timer is already stopped');
}
this.isRunning = false;
this.overallTime = this.overallTime + this._getTimeElapsedSinceLastStart();
}
reset () {
this.overallTime = 0;
if (this.isRunning) {
this.startTime = Date.now();
return;
}
this.startTime = 0;
}
getTime () {
if (!this.startTime) {
return 0;
}
if (this.isRunning) {
return this.overallTime + this._getTimeElapsedSinceLastStart();
}
return this.overallTime;
}
}
//get an instant of timer
const timer = new Timer();
timer.reset();
//send request to api
var burl = 'https://api.binance.com';
var endPoint = '/api/v3/ping';
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET',burl+endPoint,true);
ourRequest.onload = function(){
timer.stop();
console.log('ping: '+timer.getTime()+' ms')
}
timer.start();
ourRequest.send();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。