所以我是节点js的新手,我试图移植现有的API。这个函数我只是简单地使用udp来查询远程服务器并等待响应,但是当我循环大量随机生成的查询时,很多都会开始计时。
当我检查服务器日志时,我注意到并非所有查询请求都能通过。这种情况只有在我循环几百次或更多次时才会发生,当数量较小时,很少发生。
var dgram = require("dgram");
var randomstring = require("randomstring");
var xml2js = require("xml2js");
var naTools = require("./NATools.js")
var ipVersion = 5;
var apiId = 7;
/**
*
* @param queryParamArray query params
* @param callback function provided by user
*/
exports.queryServer = function(queryParamArray, callback) {
//set socket type and query string
var client = dgram.createSocket("udp4");
var transactionId = randomstring.generate({
length : 20,
charset : "alphanumeric"
});
var queryString = queryParamArray[0] + ";" + queryParamArray[1] + ";" + queryParamArray[2] + ";" + ipVersion + ";" + apiId + ";" + transactionId + ";";
var responseObject;
var bufferMsg = new Buffer(queryString);
//if the request times out, close the client and call the callback function with the response
var timeoutObject = setTimeout(function() {
client.close();
responseObject = "Error : Request timed out after " + queryParamArray[4] + " milliseconds for transaction : " + transactionId;
callback(responseObject);
}, queryParamArray[4]);
//parses the message received from the netacuity server and calls a function that generates the response objects and calls the callback function
client.on("message", function(message) {
client.close();
clearTimeout(timeoutObject);
var msg = message.toString();
var delimitedArray = msg.split(";");
//find the transactionId section, followed by the error, and then the fields - sometimes netacuity server pads with an extra field
var index = 0;
while(delimitedArray[index] != transactionId && index<delimitedArray.length){
index++;
}
if(index >= delimitedArray.length) {
responseObject = "Error : transaction id from response does not match transaction id of request.";
callback(responseObject);
return;
}
if(delimitedArray[index+1] == '') { //make sure error field is empty
var responseArray = delimitedArray.slice(index+2, delimitedArray.length-1);
var paramArray = [queryParamArray[0], transactionId, queryParamArray[2]];
naTools.generateResponseObject(paramArray, responseArray, callback);
} else {
responseObject = "Error : " + delimitedArray[index+1];
callback(responseObject);
return;
}
});
//send the request
client.send(bufferMsg, 0, bufferMsg.length, 5400, queryParamArray[3], function() {
console.log("Querying Server : " + queryString);
});
}
naTools.generateResponseObject只是一个基于paramArray [0]的switch语句,它生成一个带有数据的Object,并在其上调用传入的回调函数。
以下是测试用例:(假设所有必需的导入都在那里)
var databaseEnums = {
xxx : 3,
xxx : 4,
xxx : 5,
xxx : 6,
xxx : 7,
xxx : 8,
xxx : 9,
xxx : 10,
xxx : 11,
xxx : 12,
xxx : 14,
xxx : 15,
xxx : 17,
xxx : 18,
xxx : 19,
xxx : 24,
xxx : 25,
xxx : 26
};
var incompleteIp = "2.242.71."; //put some c class ip here
var randomIp = incompleteIp;
for(var i = 0; i < 500; i++) { //running x test queries
for(var j = 0; j < 3; j++) {
var randomNumber = j == 0 ? Math.floor(Math.random() * 3) : Math.floor(Math.random() * 6);
randomIp += randomNumber;
}
var propertyList = Object.keys(databaseEnums);
var randomPropertyName = propertyList[Math.floor(Math.random()*propertyList.length)];
var randomFeatureCode = databaseEnums[randomPropertyName];
api.queryServer([randomFeatureCode, 64, randomIp, "192.168.11.28", 2000, 5400], function(response) {
console.log(response);
});
randomIp = incompleteIp;
}
我不明白为什么有些查询无法进入服务器并且for循环超过300时会超时?有时它会发生,有时它们都会完成。我做错了什么阻止了事件循环?