javascript - Node.js ReadLine 不等待套接字连接的完整行?

标签 javascript node.js newline

我正在尝试使用 Node.js's ReadLinea socket ,就像这样:

var net = require('net');
var rl = require('readline');

this.streamServer = net.createServer(function (socket) {
    var i = rl.createInterface(socket, socket);
    i.on('line', function (line) {
        socket.write(line);
    });
});
this.streamServer.maxConnections = 1;
this.streamServer.listen(7001);

当我 telnet 到端口 7001 并开始输入文本时,它会在我按下 Enter 之前立即回显给我。

为什么 ReadLine 没有等待整行?

我也试过.question()我得到了相同的结果...在接收到任何数据时触发回调,而无需等待行尾字符。


编辑:这变得更奇怪了。当我使用 Windows telnet 客户端进行测试时,我得到了上述行为。但是,如果我使用 PuTTY 作为客户端进行测试,ReadLine 可以工作,即使在 Windows 上也是如此。我做了一些数据包捕获。也许有人可以对此有所了解?未缩进的行是来自客户端的数据。缩进的行是服务器回复。

使用 Windows Telnet

00000000  61                                               a
    00000000  61                                               a
00000001  62                                               b
    00000001  62                                               b
00000002  63                                               c
    00000002  63                                               c
00000003  64                                               d
    00000003  64                                               d
00000004  65                                               e
    00000004  65                                               e
00000005  66                                               f
    00000005  66                                               f
00000006  67                                               g
    00000006  67                                               g
00000007  68                                               h
    00000007  68                                               h
00000008  69                                               i
    00000008  69                                               i
00000009  6a                                               j
    00000009  6a                                               j
0000000A  6b                                               k
    0000000A  6b                                               k
0000000B  6c                                               l
    0000000B  6c                                               l
0000000C  6d                                               m
    0000000C  6d                                               m
0000000D  6e                                               n
    0000000D  6e                                               n
0000000E  6f                                               o
    0000000E  6f                                               o
0000000F  70                                               p
    0000000F  70                                               p
00000010  0d 0a                                            ..
    00000010  0d 0a                                            ..
00000012  0d 0a                                            ..
    00000012  0d 0a                                            ..
00000014  0d 0a                                            ..
    00000014  0d 0a                                            ..
00000016  61                                               a
    00000016  61                                               a
00000017  73                                               s
    00000017  73                                               s
00000018  64                                               d
    00000018  64                                               d
00000019  66                                               f
    00000019  66                                               f
0000001A  0d 0a                                            ..
    0000001A  0d 0a                                            ..
0000001C  61                                               a
    0000001C  61                                               a
0000001D  73                                               s
    0000001D  73                                               s
0000001E  64                                               d
    0000001E  64                                               d
0000001F  66                                               f
    0000001F  66                                               f
00000020  0d 0a                                            ..
    00000020  0d 0a                                            ..

使用 PuTTY

00000000  ff fb 1f ff fb 20 ff fb  18 ff fb 27 ff fd 01 ff ..... .. ...'....
00000010  fb 03 ff fd 03                                   .....
    00000000  ef bf bd ef bf bd 1f ef  bf bd ef bf bd 20 ef bf ........ ..... ..
    00000010  bd ef bf bd 18 ef bf bd  ef bf bd 27 ef bf bd ef ........ ...'....
    00000020  bf bd 01 ef bf bd ef bf  bd 03 ef bf bd ef bf bd ........ ........
    00000030  03                                               .
00000015  61 62 63 64 65 66 67                             abcdefg
0000001C  0d 0a                                            ..
    00000031  61 62 63 64 65 66 67                             abcdefg
    00000038  0d 0a                                            ..
0000001E  61 73 64 66                                      asdf
00000022  0d 0a                                            ..
    0000003A  61 73 64 66                                      asdf
    0000003E  0d 0a                                            ..
00000024  61 73 64 66                                      asdf
00000028  0d 0a                                            ..
    00000040  61 73 64 66                                      asdf
    00000044  0d 0a                                            ..
0000002A  0d 0a                                            ..
    00000046  0d 0a                                            ..

最佳答案

这是 node.js 中的一个错误,ReadLine 的接口(interface)在每个 'data' 事件上调用 _normalWrite() 并且 _normaWrite 有一条注释说它应该尝试在换行符上换行,但目前它是 just calls _onLine() .

类似的东西应该会为你解决这个问题:

i._normalWrite = function(b) {
    if(b == undefined) {
        return;
    }
    if(!this._line_buffer) {
        this._line_buffer = '';
    }
    this._line_buffer += b.toString();
    if(this._line_buffer.indexOf('\n') !=-1 ) {
        var lines = this._line_buffer.split('\n');
        // either '' or the unfinished portion of the next line
        this._line_buffer = lines.pop();
        lines.forEach(function(line) {
            this._onLine(line + '\n');
        }, this);
    }
};

我没有对此进行测试,可能还需要考虑 \r 。请让我知道它是否适合您,如果适合,那么我们中的一个应该向 Node 发送拉取请求。

关于javascript - Node.js ReadLine 不等待套接字连接的完整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9962197/

相关文章:

javascript - 没有工作功能 ng-class 和 ng-repeat

javascript - 在文本框中输入值时不显示值

javascript - 我可以直接向 Express 应用程序发送请求,而不是通过 http 吗?

java - 在字节数组中捕获 <CR><LF>

javascript - 将数字序列添加到 div

javascript - 使用 for 循环加载 Jssor Slider

mysql - Node.js 在使用变量之前尝试使用 async/await 从 mysql select 获取数据

php - Node.js 中的 AES 加密 PHP 中的解密。失败。

python-3.x - 将数组行 append 到 Python 3 中的 .txt 文件

vim - 如何使用 Vim 根据模式将文本拆分为多行?