我的代码是在 TCP Node JS 上编写的,除了一些多线程之外,它工作正常。例如,在第一个套接字事务期间,另一个套接字未连接到服务器
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());
}
我们将高度赞赏您在这方面的回应
最佳答案
Javascript 代码在 Node.js 中的单线程中执行。您的 newConnection
函数会阻塞该线程(Math.random()
可能返回 0,但概率非常低)。
关于javascript - TCP NODE JS 多线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23650614/