我正在尝试在套接字上使用REPL,就像telnet连接一样,但是当我按下CTRL + C或CTRL + D,或者产生错误时,套接字连接挂起并且不响应任何内容。
node.js代码是:
var net = require("net");
var connections = 0;
// Socket port 5001
net.createServer(function (socket) {
connections += 1;
var server = require("repl").start(
'session:' + connections + ' > ',
socket
);
server.rli.on('exit', function() {
socket.end();
});
}).listen(5001);
// Console
var server = require("repl").start('session:' + (++connections) + ' > ', null);
从我运行的其他终端:
telnet localhost 5001
有什么问题?
答案 0 :(得分:0)
也许是linux问题。
我试过Telnet server example with node。
当我在窗口上运行时,我按下的每个键都由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);