也许是linux的问题。
我尝试了一个带有 node 的 Telnet 服务器示例。当我在窗口上运行时,我按下的每个键都由receiveData
函数处理,但在 linux 中没有。在linux中,每行处理一行,而不是每个字符一个字符。
这是我探测过的代码:
var net = require('net');
var sockets = [];
/*
* Cleans the input of carriage return, newline
*/
function cleanInput(data) {
return data.toString().replace(/(\r\n|\n|\r)/gm,"");
}
/*
* Method executed when data is received from a socket
*/
function receiveData(socket, data) {
var cleanData = cleanInput(data);
if(cleanData === "@quit") {
socket.end('Goodbye!\n');
}
else {
for(var i = 0; i<sockets.length; i++) {
if (sockets[i] !== socket) {
sockets[i].write(data);
}
}
}
socket.write('Data: ' + data);
}
/*
* Method executed when a socket ends
*/
function closeSocket(socket) {
var i = sockets.indexOf(socket);
if (i != -1) {
sockets.splice(i, 1);
}
}
/*
* Callback method executed when a new TCP socket is opened.
*/
function newSocket(socket) {
sockets.push(socket);
socket.write('Welcome to the Telnet server!\n');
socket.on('data', function(data) {
receiveData(socket, data);
})
socket.on('end', function() {
closeSocket(socket);
})
}
// Create a new server and provide a callback for when a connection occurs
var server = net.createServer(newSocket);
// Listen on port 8888
server.listen(8888);