Socket io vs Nodejs: died when 2k concurrent user? - javascript

My project have one module. It need to be real-time push data from json file from Server to Client. I am using Socket IO and Nodejs for listenning client. My idea is server nodejs setInterval every 5 second and check current time, and if valid then read json file and emit to all client. I try with 2k client and socket io and nodejs died. I read data from folder with name ttkqxs, file name format is 1.json, 2.json. Read and push very good but with 2k client and more i have trouble. Here is my code, what problem with it ???
// Dependencies
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var async = require('async');
// Time config
var MN_START_HOUR = 16,
MN_START_MIN = 5,
MN_END_MIN = 45,
MT_START_HOUR = 17,
MT_START_MIN = 5,
MT_END_MIN = 45,
MB_START_HOUR = 18,
MB_START_MIN = 5,
MB_END_MIN = 45;
var intervalTime = 5000;
var MB_ID = 22;
var MB = 1;
var MT = 2;
var MN = 3;
var firstTimeMb = true;
var firstTimeMt = true;
var firstTimeMn = true;
// Server ready
http.listen(3113, function () {
console.log('Server listening on port: 3113 ');
var timer = setInterval(function () {
var date = new Date();
var min = date.getMinutes();
var sec = date.getSeconds();
var hour = date.getHours();
console.log("CurrentTime: " + hour + ":" + min + ":" + sec);
// Mien Bac
if (hour === MB_START_HOUR && min >= MB_START_MIN && min <= MB_END_MIN) {
if (firstTimeMb) {
firstTimeMb = false;
emitRegionData(MB, 'first_time_' + MB);
}
emitRegionData(MB, 'return_g_result_' + MB);
emitProvincesData(MB);
}
// Miền Trung
if (hour === MT_START_HOUR && min >= MT_START_MIN && min <= MT_END_MIN) {
if (firstTimeMt) {
firstTimeMt = false;
emitRegionData(MT, 'first_time_' + MT);
}
emitRegionData(MT, 'return_g_result_' + MT);
emitProvincesData(MT);
}
//Miền Nam
if (hour === MN_START_HOUR && min >= MN_START_MIN && min <= MN_END_MIN) {
if (firstTimeMn) {
firstTimeMn = false;
emitRegionData(MN, 'first_time_' + MN);
}
emitRegionData(MN, 'return_g_result_' + MN);
emitProvincesData(MN);
}
// reset first time
if (hour == 23) {
firstTimeMb = true;
firstTimeMt = true;
firstTimeMn = true;
}
}, intervalTime);
});
/**
* Get province data from json file and emit to all client
* Desktop: Use in home page, province page
* Mobile: Only use in province page
* #param {int} pid Province id
* #returns
*/
function emitProvinceData(pid) {
var fileName = __dirname + '/ttkqxs/' + pid + '.json';
fs.exists(fileName, function () {
fs.readFile(fileName, function (err, content) {
if (err) {
throw err;
}
io.sockets.emit('return_p_result_' + pid, content);
});
});
}
/**
* Get province data from json file and emit to all client
* Desktop: Use in home page, province page
* Mobile: Only use in province page
* #param {int} pid Province id
* #returns
*/
function emitProvincesData(rid) {
var fileName = __dirname + '/ttkqxs/mien' + rid + '.json';
fs.exists(fileName, function () {
fs.readFile(fileName, function (err, content) {
if (err) {
throw err;
}
var ids = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(content)));
for (var i in ids) {
emitProvinceData(ids[i].id);
}
});
});
}
/**
* Emit Region data
* Use in mobile for multiple province
* #param {int} rid Region id
* #returns
*/
function emitRegionData(rid, eventName) {
var fileName = __dirname + '/ttkqxs/mien' + rid + '.json';
fs.exists(fileName, function () {
fs.readFile(fileName, function (err, content) {
if (err) {
throw err;
}
var ids = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(content)));
var files = [];
for (var i in ids) {
var pFileName = __dirname + '/ttkqxs/' + ids[i].id + '.json';
files.push(pFileName);
}
async.map(files, readAsync, function (err, results) {
io.sockets.emit(eventName, JSON.stringify(results));
});
});
});
}
/**
* Read file Async
* #param {string} file File Path
* #param {function} callback
* #returns
*/
function readAsync(file, callback) {
fs.readFile(file, 'utf8', callback);
}

Related

Node JS: res.send is not a function

I'm in the midst of making my first simple counter strike api bot with nodeJS, however I'm running into some problems with the res.send function. When I use it it throws the following error: "res.send is not a function". I've tried moving the res.send outside of the request, however that only updates my data after I refresh the page. Any help would be appreciated as I've been stuck on this problem for a while now.
My code :
const path = require('path')
const express = require('express')
var request = require("request")
/*const getSkinfo = require('./wyrskin')*/
const app = express()
const publicDirectoryPath = path.join(__dirname, '/public')
var skins_str = "empty"
app.use(express.static(publicDirectoryPath))
/*var string_skin = (JSON.stringify(getSkinfo))*/
app.get('/skin', (req, res) => {
request('https://api.steamapis.com/market/items/730?api_key=xyz', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
var skins = []
var score = 0
var i;
for (i = 0; i < body.data.length; i++) {
var name = body.data[i].market_name
var price = body.data[i].prices
var img_url = body.data[i].image
if (name.includes('Factory New') && !name.includes('StatTrak')) {
skins.push(name + "," + price.avg + "^" + img_url)
}
}
var num1 = Math.floor(Math.random() * 1215);
var num2 = Math.floor(Math.random() * 1215);
var out1 = "A : " + skins[num1]
var out2 = "B : " + skins[num2]
var mySubString1 = out1.substring(
out1.lastIndexOf(",") + 1,
out1.lastIndexOf("^")
);
var price1 = parseFloat(mySubString1)
var mySubString2 = out2.substring(
out2.lastIndexOf(",") + 1,
out2.lastIndexOf("^")
);
var price2 = parseFloat(mySubString2)
skins_str = (out1 + " ~ " + out2)
console.log(skins_str)
res.send({ skins: skins_str })
})
})
app.listen(3000, () => {
console.log('Server is up on port 3000.')
})
Thank you !
This isn't really how I would proxy a request from a node server. Checkout the node-fetch npm package. Promises really clean up the code.
But the error in your code is that you have another res variable in the scope of this function. If you rename it to response it should work?
Also use res.json() instead if you are just sending an object in your response.
Hope this helps!
app.get('/skin', (req, res) => {
request('https://api.steamapis.com/market/items/730?api_key=xyz', { json: true }, (err, response, body) => {
if (err) { return console.log(err); }
var skins = []
var score = 0
var i;
for (i = 0; i < body.data.length; i++) {
var name = body.data[i].market_name
var price = body.data[i].prices
var img_url = body.data[i].image
if (name.includes('Factory New') && !name.includes('StatTrak')) {
skins.push(name + "," + price.avg + "^" + img_url)
}
}
var num1 = Math.floor(Math.random() * 1215);
var num2 = Math.floor(Math.random() * 1215);
var out1 = "A : " + skins[num1]
var out2 = "B : " + skins[num2]
var mySubString1 = out1.substring(
out1.lastIndexOf(",") + 1,
out1.lastIndexOf("^")
);
var price1 = parseFloat(mySubString1)
var mySubString2 = out2.substring(
out2.lastIndexOf(",") + 1,
out2.lastIndexOf("^")
);
var price2 = parseFloat(mySubString2)
skins_str = (out1 + " ~ " + out2)
console.log(skins_str)
res.json({ skins: skins_str })
})
})
app.listen(3000, () => {
console.log('Server is up on port 3000.')
})

Server crash, sending message at restart (Node.js / Socket.io)

Hey guys i am doing a message system all is working fine but now i want to add the thing that if the server crash and restart the message that have been send during this time will be send at the restart of the server. I am trying to save the information of the message in the client side and make a "waiting" system that will wait to a server response. So i wanted to know how can i do that "waiting" system because now i am doing like that :
while (socket.connected === false) {
}
But that make bug the client because the infinit loop is way too fast ... so is that possible to set a timer ? (i have already tried but i dind't find how to make a good timer in a loop).
Or maybe i am totaly wrong and i haven't to do a waiting system but an other thing so tell me if my technic will not work or if there is one better :)
So here is my code :
Client.js (startTchat is called when someone is connected)
(function($){
var socket = io.connect('http://localhost:1337');
var lastmsg = [];
var me_id = [];
var friend_ = [];
var conv_ = [];
var isPlace_ = [];
var isLocation_ = [];
var me_ = [];
var my_id;
startTchat = function(user_id, username, friend_id, conv_id, isPlace, isLocalisation) {
my_id = user_id;
socket.emit('login_chat', {
id : user_id,
username : username,
friend : friend_id,
conv : conv_id,
isPlace : isPlace,
isLocalisation : isLocalisation,
})
};
/**
* Error
*/
socket.on('error', function(err){
alert(err);
});
/**
* Messages
*/
$('#chat_form').submit(function(event){
var a = 0;
while (socket.connected === false) {
}
event.preventDefault();
console.log('ME', my_id, 'TAB', me_id);
socket.emit('new_msg', {message: $('#message').val() }, me_id[my_id], friend_[my_id], conv_[my_id], isPlace_[my_id], isLocation_[my_id], me_[my_id]);
if (a === 1) {
console.log('HEYYYYYYYYYY', my_id);
}
$('#message').val('');
$('#message').focus();
});
socket.on('new_msg', function(message, me, id_receiver, id_transmiter){
if (me.id === id_receiver || me.id === id_transmiter) {
if (lastmsg != message.user.id) {
$('#new_message').append('<span class="time_date"> ' + message.h + ' : ' + message.m + ' | ' + message.y + '-' + message.m + '-' + message.d + ' | ' + message.user.username + '</span>'
+ '<p>' + message.message + '</p>\n'
);
lastmsg = message.user.id;
} else {
$('#new_message').append('<p>' + message.message + '</p>'
);
}
}
});
/**
* Login
*/
socket.on('new_user', function(user, friend, conv, isPlace, isLocation){
me_id[user.id] = user.id;
friend_[user.id] = friend;
conv_[user.id] = conv;
isPlace_[user.id] = isPlace;
me_[user.id] = user;
isLocation_[user.id] = isLocation;
$('#new_user').append('<div class="chat_list active_chat" id="' + user.id + '">\n' +
' <div class="chat_people">\n' +
' <div class="chat_img"> <img src="https://ptetutorials.com/images/user-profile.png" alt="sunil"> </div>\n' +
' <div class="chat_ib">\n' +
' <h5>' + user.username + ' <span class="chat_date">Id : ' + user.id + '</span></h5>\n' +
' </div>\n' +
' </div>\n' +
' </div>');
});
/**
* Disconnect
*/
socket.on('disc_user', function(user){
$('#' + user.id).remove();
})
})(jQuery);
And server.js :
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'msg';
MongoClient.connect(url, function(err, client) {
if (err)
throw err;
console.log('MongoDB connected ...');
httpServer = http.createServer(function(req, res) {
console.log('This is a test');
res.end('Hello World');
});
httpServer.listen(1337);
var io = require('socket.io').listen(httpServer);
var users = {};
var messages = [];
io.sockets.on('connection', function (socket) {
const collection = client.db(dbName).collection('MessageUser');
var me = false;
var friend = false;
var conv = false;
var isPlace = false;
var room = false;
var isLocalisation = false;
for(var k in users) {
socket.emit('new_user', users[k]);
}
/**
* Login
*/
socket.on('login_chat', function (user) {
me = user;
friend = user.friend;
isPlace = user.isPlace;
conv = user.conv;
isLocalisation = user.isLocalisation;
if (isPlace === 0) {
room = user.conv;
} else {
room = user.conv + '-Place';
}
socket.join(room);
//console.log('New user : ', me.username, ' - id : ', me.id);
users[me.id] = me;
io.sockets.emit('new_user', me, friend, conv, isPlace, isLocalisation);
});
/**
* Disconnect
*/
socket.on('disconnect', function() {
if (!me) {
return false;
}
delete users[me.id];
io.sockets.emit('disc_user', me);
});
/**
* Message receive
*/
socket.on('new_msg', function(message, me_id, friend_, conv_, isPlace_, isLocalisation_, me_){
if (message.message !== '') {
message.user = me;
date = new Date();
message.h = date.getHours();
message.m = date.getMinutes();
message.y = date.getFullYear();
message.m = date.getMonth();
message.d = date.getDate();
console.log(message);
messages.push(message);
msg = {};
msg.content = message.message;
msg.sendAt = new Date();
msg.idTransmitter = me.id;
if (isPlace === 0) {
msg.idReceiver = friend;
} else {
msg.idReceiver = conv;
}
msg.idConversation = conv;
msg.isPlace = isPlace;
msg.isLocalisation = isLocalisation;
collection.insertOne(msg);
console.log('---1---', msg.idReceiver, '---2---', msg.idTransmitter, '---3---', me);
io.to(room).emit('new_msg', message, me, msg.idReceiver, msg.idTransmitter);
}
});
});
});
ps : Tell me if you need more info, sorry if i forget something that my first time using js, node and socket.io :)
while (socket.connected === false) {
}
Don't do that, it will block your page and hold your processor to 100%.
Instead, use setTimeout. It's the equivalent of sleep in javascript. You need to refactor your code to call setTimeout in a recursive fashion, and count the number of "retries" (if you want to stop at some point).
Code:
$('#chat_form').submit(function(event){
var retries = 0, max_retries = 10;
function tryNewMessage() {
if (socket.connected === false) {
if (retries >= max_retries) return; //handle max_retries properly in your code
//this is where you sleep for 1 second, waiting for the server to come online
setTimeout(tryNewMessage, 1000);
retries++;
}
else {
var a = 0;
event.preventDefault();
console.log('ME', my_id, 'TAB', me_id);
socket.emit('new_msg', {message: $('#message').val() }, me_id[my_id], friend_[my_id], conv_[my_id], isPlace_[my_id], isLocation_[my_id], me_[my_id]);
if (a === 1) {
console.log('HEYYYYYYYYYY', my_id);
}
$('#message').val('');
$('#message').focus();
}
}
tryNewMessage();
});

When using the expressjs in Nodejs, the server keeps close_wait status after a few thousands of requests

I put the code below onto my server:
var express = require('express');
var app = express();
var mysql = require('mysql');
var db = mysql.createPool({host:'192.168.1.234', user:'root', password:'123456', database:'db_xuyou_test', port:3306});
app.post('/query', function (req, res){
s = {};
platform = Number(req.query['platform']) * 100000000;
platform_upper = platform + 99999999;
appv = Number(req.query['appv']) * 10000;
resource = Number(req.query['res']) * 1;
id = platform + appv + resource;
resource_upper = platform + appv + 9999;
sql = 'select appv from (select min(id) as min_id from download where id >= ? and id <= ?) as a inner join download on a.min_id = download.id';
db.query(sql, [platform, platform_upper], function(err, row) {
if (err) {
console.error(err);
res.end();
return;
}
if (row.length <= 0) {
s['code'] = 2;
s['message'] = 'update!';
res.send(s);
console.log(s);
return;
}
if (Number(row[0]['appv']) > appv) {
s['code'] = 2;
s['message'] = 'update!';
res.send(s);
console.log(s);
return;
}
sql = 'select res as version, versionName, ip, port, filename from download where id > ? and id <= ? order by id';
db.query(sql, [id, resource_upper], function(err2, resRow) {
if (err2) {
console.error(err2);
res.end();
return;
}
if (resRow.length <= 0) {
s['code'] = 0;
s['message'] = '';
res.send(s);
console.log(s);
return;
}
s['code'] = 1;
s['message'] = '';
s['versionData'] = resRow;
console.log(s);
res.send(s);
});
});
});
app.listen(3000, '0.0.0.0');
And I wrote a test which sends thousands of requests to the server. But very weirdly, the the server suddenly ran into close_wait status after processing a few thousands of requests.
As the code describes, it processes the request by selecting the data from the database and send the result back to the clients and the procedure will end.
Why it turns out to be close_wait?? How to solve it?
Thanks in advance.

node.js + serial code not work consistently

i tried to calculate root mean square using node.js, trying to be smart to make some kind of high resolution realtime measurement of stuff from serial port.
the code doesnt really matter. but each time i try to run this multiple times but it output totally different rms output value, sometimes very big, sometimes very small, NaN, etc.
the question is:
why? how? is there any node.js quirk i miss? like inter-process shared memory allocation?
it's fixed after i rewrite the program(some edit) or waiting for some time
re running a program should do the same thing everytime, right?
btw i'm using nodeserial for serial communication, at 500k baud
actual code may be posted later if you want
edit: the code
var math = require('mathjs');
var fs = require('fs');
// serving the static html "client"
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
// socket.io
var app = require('express')();
var http2 = require('http').Server(app);
var io = require('socket.io')(http2);
io.on('connection', function(socket) {
console.log('client connected');
});
/* CONSOLE INTERFACE, USED TO MANUALLY INSERT SOME KIND OF VARIABLES*/
//stdin interaction
var stdin = process.openStdin();
var command = '';
var commandmode = false;
var bufferdata;
var command = 0;
//serial stuff
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
//portName = process.argv[2];
portName = "COM4"
//parsing commandline argument as comport name
var myPort = new SerialPort(portName, {
baudRate: 500000, // choose between: 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1800, 1200, 600, 300, 200, 150, 134, 110, 75, or 50
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\n")
});
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);
var datacount = 0;
function showPortOpen() {
console.log('port open. Data rate: ' + myPort.options.baudRate);
}
var timestamp;
var timestampold;
var dt;
function pad(num, size) {
var s = "0000" + num;
return s.substring(s.length - size);
}
function padd(num, size) {
var s = "00000000" + num;
return s.substring(s.length - size);
}
var vrms = [];
var irms = [];
var torq = [];
var speed = [];
var vraw = [];
var iraw = [];
function sendSerialData(data) {
if (datacount === 0) {
timestampold = Date.now();
}
if (datacount === 10) {
console.log(data);
}
if (datacount === 1000) {
timestamp = Date.now();
dt = timestamp - timestampold;
var samplerate;
var datalength = data.length + 1;
samplerate = datacount * 1000 / dt;
var troughput = datalength * samplerate;
var troughputp = 800 * troughput / 500000;
console.log(datacount + " data has been recorded. time elapsed:" + dt + ", samplerate:" + samplerate + " troughput:" + troughput + "/" + troughputp + "%");
}
var parseddata = data.split(",");
vraw.push(parseInt(parseddata[0], 16));
iraw.push(parseInt(parseddata[1], 16));
torq.push(parseInt(parseddata[2], 16));
speed.push(parseInt(parseddata[3], 16));
rmsfast(vraw[datacount], iraw[datacount]);
//var rawlog = padd(datacount,8) + "," + pad(vraw[datacount],4) + ","+ pad(iraw[datacount],4) +","+ pad(speed[datacount],4)+","+ pad(torq[datacount],4);
//var rawlog = padd(datacount, 8) + "," + vraw[datacount] + "," + iraw[datacount] + "," + speed[datacount] + "," + torq[datacount] + ">>" + vrms[datacount] + "," + irms[datacount];
//fs.appendFileSync('raw.txt', rawlog);
//console.log(rawlog);
++datacount;
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}
var varray = [0];
// zero setup;
var mid = 2048; //ideal; changed on zerocross detection
/* the data from microcontroller is as such: VVV,III,TTT,NNN
CC = COMMAND/RUNNING MODE
VVVV = VOLTAGE READING
IIII = CURRENT READING
TTTT = TORQUE READING
NNNN = SPEED (RPM) READING
Z = IS ZERO(0=false,1=voltage,2=current)
delta t between data (microsecond);
*/
var bufferV = [0];
var bufferI = [0];
var datacount;
var dataindex; // index of data called on the below stuff
var rmslength = 80;
var sigmaVsquared = 0;
var sigmaIsquared = 0;
var lastVvalue = 0;
var lastIvalue = 0;
var Vrms = 0;
var avgV = 0;
var avgI = 0;
function rmsfast(newVoltage, newCurrent) { // example use = rmsfast(data,buffer,)
var newSqVoltage = Math.pow(newVoltage, 2);
var newSqCurrent = Math.pow(newCurrent, 2);
bufferV.push(newSqVoltage);
if (bufferV.length === 80) {
lastVvalue = bufferV.shift();
} else {
//lastVvalue = newSqVoltage;
}
avgV += ((newSqVoltage - lastVvalue) / 80);
Vrms = math.sqrt(avgV);
bufferI.push(newSqCurrent);
lastIvalue = bufferI.shift();
avgI += (newSqCurrent - lastIvalue) / 80;
var Irms = math.sqrt(avgI);
var darn = math.sqrt(4000000);
if (datacount < 1000) {
var darz = Number.isInteger(newSqVoltage);
var vavg = avgV;
var vrmss = vrms;
console.log(newVoltage + "\t---" + avgV + "\t---" + Vrms);
}
if (datacount === 1000) {
myPort.close(process.exit());
}
vrms.push(Vrms);
irms.push(Irms);
}
server.listen(2000); // listening to the port
stdin.addListener("data", function(cons) { //"listening" to stdin
command = cons.toString().trim();
});

Node JS UDP Datagram API hangup when scaling

So I'm new to node js and I'm trying to port over an existing API. This one function I'm working on simply udp queries a remote server and waits for a response, but when I loop a ton of randomly generated queries, a lot of them start timing out.
When I check the server logs, I notice that not all of the query requests make it through. This only happens when I loop it several hundred times or more, very rarely when it's a smaller number.
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 is just a switch statement based on paramArray[0] and generates an Object with the data and calls the passed in callback function on it.
Here is the test case : (Assume all the required imports are there)
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;
}
I don't understand why some of the queries don't make it to the server and time out when the for loop gets past 300? Sometimes it happens, sometimes they all complete. Is there something I did wrong that's blocking the event loop?

Categories

Resources