我已经构建了一个简单的 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);