Hello, I am building a telnet server with node js.
Has anyone ever used the
readline module with socket as input and output?
i.e.
readline.createInterface(socket, socket);
It seems everything goes wrong when I use socket as I/O
Here is my code:
===============================================
/*
* Callback method executed when a new TCP socket is opened.
*/
function newSocket(socket) {
var option = {
input: socket,
output: socket,
terminal:true
};
var rl =readline.createInterface(option);
rl.write("Welcome to the Telnet server!\n");//Works fine
rl.setPrompt(">");
rl.prompt();
rl.on('line', function (line){//On reading a line!
line = line.trim();
if(line === "@quit") {
socket.write('Goodbye!\n');
closeSocket(socket);//The function is defined elsewhere
}else if (line === "enable") {
socket.write("Enter enable mode!\n")
rl.setPrompt("#");
}else{
socket.write('echo:'+line+'\n');
}
rl.prompt();
});
};
// Create a new server and provide a callback for when a connection occurs
var server = net.createServer(newSocket);
server.listen(8888);
============================================
After I telnet into the server and input 'abc', the results shown in the
client are:
Welcome to the Telnet server!
>abc //Input abc
abc
>echo:abc
>echo:echo:abc
============================================
And the
server side:
Server has started!
Read a line!
Read a line!
============================================
It seems it reads twice after I send my input, how could this happen?
Any alternative for building a telnet server with completion function of NodeJS is also welcome.
Thank you so much!