我試圖讓一個客戶端開始連續發送空字符串。我無法讓客戶在第四次嘗試時開始。請參閱「//從這裏開始的問題」。nodejs不踢無禮
控制檯消息我得到
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;
}
儘量不要用正則表達式重新發明輪子,而是對收到的數據使用限制,例如最大和最小長度的消息,也嘗試一些驗證和消毒模塊,刪除它不是安全的方式。 – Gntem
嘿GeoPhoenix,我有正則表達式,但我沒有使用它們。 – Poliquin