我已经建立了一个简单的TCP服务器,需要将客户端输入与存储在变量中的硬编码字符串进行比较。
然而,data == username
总是失败。
为什么?我该怎么办?
示例:
var authenticateClient = function(client) {
client.write("Enter your username:");
var username = "eleeist";
client.on("data", function(data) {
if (data == username) {
client.write("username success");
} else {
client.write("username failure");
}
});
}
var net = require("net");
var server = net.createServer(function(client) {
console.log("Server has started.");
client.on("connect", function() {
console.log("Client has connected.");
client.write("Hello!");
authenticateClient(client);
});
client.on("end", function() {
console.log("Client has disconnected.");
});
}).listen(8124);
data
包含什么?它是否包含结尾的换行符? - Sjoerd