Javascript nodejs不会踢死球

Javascript nodejs不会踢死球,javascript,node.js,Javascript,Node.js,我正试图让一个连续发送空字符串的客户端启动。我无法让客户在第四次尝试时启动。请参见“//问题从这里开始” 我收到的控制台消息 Removed newline: consecutiveWarningCount: 0 Removed newline: consecutiveWarningCount: 1 Removed newline: consecutiveWarningCount: 2 Removed newline: consecutiveWarningCount: 3 Removed

我正试图让一个连续发送空字符串的客户端启动。我无法让客户在第四次尝试时启动。请参见“//问题从这里开始”

我收到的控制台消息

Removed newline: 
consecutiveWarningCount: 0
Removed newline: 
consecutiveWarningCount: 1
Removed newline: 
consecutiveWarningCount: 2
Removed newline: 
consecutiveWarningCount: 3
Removed newline: 
consecutiveWarningCount: 4
Removed newline: 
consecutiveWarningCount: 5
代码

//Load the tcp library
var net = require('net');

//Keep track of connected clients
var clients = [];

// Vars
var ADDRESS = '127.0.0.1';
var PORT = 3001;
var consecutiveWarningCount = 0;

//Start TCP Server
var s = net.createServer(function(cl) { 
  //console.log('Server connected');
  cl.setEncoding('utf8');
  cl.name = cl.remoteAddress + ":" + cl.remotePort;

  //Put the client into the list
  clients.push(cl);
  console.log('Pushed ' + cl.name + ' into client array');

  cl.on('end', function() {
    clients.splice(clients.indexOf(cl), 1);
    console.log('Server disconnected');
  });

  cl.on('data', function(data) {
    data = removeCarriageReturns(data);      //PROBLEM STARTS HERE.
    if (data.length == 0) {
      cl.write("You sent an empty string\r\n");
      consecutiveWarningCount += 1;
      console.log('consecutiveWarningCount: ' + consecutiveWarningCount);
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 1)) {
      cl.write("You sent an empty string again. This is your first warning.");
      consecutiveWarningCount += 1;
      console.log('consecutiveWarningCount: ' + consecutiveWarningCount);
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 2)) {
      cl.write("You sent an empty string again. This is your second warning.");
      consecutiveWarningCount += 1;
      console.log('consecutiveWarningCount: ' + consecutiveWarningCount);
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 3)) {
      cl.write("You sent an empty string again. This is your last warning.");
      cl.write("You will be disconnected if you send one more empty string");
      consecutiveWarningCount += 1;
      console.log('consecutiveWarningCount: ' + consecutiveWarningCount);
    } 
    else if ((data.length == 0) && (consecutiveWarningCount == 4)) {
      cl.destroy();
      console.log('Kicked ' + cl.remoteAddress);
    }
    else {
      console.log('Server received "' + data + '" from ' + cl.remoteAddress + ':' + cl.remotePort);
      cl.write('Client wrote ' + data ); 
      if (consecutiveWarningCount > 0) {
        consecutiveWarningCount -= 1;
      }
    }
  }); 

}); //End server

s.listen(PORT, function() { //'listening' listener

  //Start up information about the server
  console.log('-----------------------------');
  console.log('Server bound to port: ' + PORT);
  console.log('-----------------------------');
  console.log('Server listening...');
});

// Custom functions
var removeCarriageReturns = function(str) {
  var regexp = /\r\n/g;
  var regexp_carriagereturn = /\r/g;
  var regexp_newline = /\n/g;

  if (str.charAt(str.length-1) == "\n") {
    str = str.substring(0, str.length-2);
    console.log("Removed newline: " + str);
    if (str.charAt(str.length-1) == "\r") {
      str = str.substring(0, str.length-2);
      console.log("Removed carriage return:" + str);
    }
  }
  return str;
}
查看代码(假设
data.length
等于零):

尝试以以下方式重写代码:

if (data.length == 0) {
  if (consecutiveWarningCount == 0) {
    //You sent an empty string
  } else if (consecutiveWarningCount == 1) {
    //You sent an empty string again. This is your first warning
  } //... and so on
}

尽量不要用正则表达式重新发明轮子,而是对接收到的数据使用限制,例如最大和最小长度消息,还可以尝试一些验证和清理模块,删除
\r\n
这不是安全的方法。嘿,GeoPhoenix,我有正则表达式,但我没有使用它们。嗨,祖布,谢谢。我有两个并发的telnet连接。我触发了一个被摧毁,但碰巧两个都被杀了。有什么想法吗?移动“var ConcertiveWarningCount=0;”在“var s=net.createServer(function(cl)…”函数中,它现在似乎可以工作了。是的。我已经开始编写关于如何使用客户端IP解决此问题的详细答案,但这比我想象的要容易得多:)
if (data.length == 0) {
  if (consecutiveWarningCount == 0) {
    //You sent an empty string
  } else if (consecutiveWarningCount == 1) {
    //You sent an empty string again. This is your first warning
  } //... and so on
}