Javascript code is executed in a single thread in node.js. Your newConnection
function blocks this thread (Math.random()
may return 0, but probability is very low).
TCP NODE JS MULTITHREADING ISSUE
-
22-07-2023 - |
题
My code is written on TCP Node JS and its working fine except some multithreading. The other socket is not connected to the server during the first socket transaction for E.g
Code:-
// Start a TCP Server
var clientServer = net.createServer(function (socket) {
socket.setEncoding('UTF-8');
socket.on('data',function(data) {
if(tryParseJSON(data) === false){
if(data.length != 0){
transferDataRequest.call({},data,socket);
}else{
response = JSON.stringify({Message:'Invalid JSON Object',Response:'Error',result:data});
response = response+addition_response;
socket.write(response);
}
}else{
var output = JSON.parse(data);
for(var i in output){
switch(i){
case "newConnection":
newConnection.call({},output[i],socket);
break;
}
}
}
});
}).listen(3000,"10.1.28.61");
// Put a friendly message on the terminal of the server.
console.log("Sever listing at 3000 port\n");
newConnection = function(data,socket){
var i = 0;
do {
i++;
console.log(1);
}
while (i = Math.random());
}
Your response in this regard it will be highly appreciated
解决方案
不隶属于 StackOverflow