小编典典

NodeJS:处理TCP套接字流的正确方法是什么?我应该使用哪个定界符?

node.js

从我的理解在这里,“V8拥有世代垃圾收集器。物件的移动风靡随机,节点不能得到一个指向原始字符串数据写入到插座。”
因此,我不应该将来自TCP流的数据存储在字符串中,特别是当该字符串变得大于Math.pow(2,16)字节时。(希望我到目前为止是..)

那么,处理来自TCP套接字的所有数据的最佳方法是什么?到目前为止,我一直在尝试_:_:_用作定界符,因为我认为它在某种程度上是唯一的,不会缠结其他事物。

将要收集的数据样本是 something_:_:_maybe a large text_:_:_ maybe tons of lines_:_:_more and more data

这是我尝试做的:

net = require('net');
var server = net.createServer(function (socket) {
    socket.on('connect',function() {
        console.log('someone connected');
        buf = new Buffer(Math.pow(2,16));  //new buffer with size 2^16
        socket.on('data',function(data) {
            if (data.toString().search('_:_:_') === -1) {    // If there's no separator in the data that just arrived...
                buf.write(data.toString());   // ... write it on the buffer. it's part of another message that will come.
            } else {        // if there is a separator in the data that arrived
                parts = data.toString().split('_:_:_'); // the first part is the end of a previous message, the last part is the start of a message to be completed in the future. Parts between separators are independent messages
                if (parts.length == 2) {
                    msg = buf.toString('utf-8',0,4) + parts[0];
                    console.log('MSG: '+ msg);
                    buf = (new Buffer(Math.pow(2,16))).write(parts[1]);
                } else {
                    msg = buf.toString() + parts[0];
                    for (var i = 1; i <= parts.length -1; i++) {
                        if (i !== parts.length-1) {
                            msg = parts[i];
                            console.log('MSG: '+msg);
                        } else {
                            buf.write(parts[i]);
                        }
                    }
                }
            }
        });
    });
});

server.listen(9999);

每当我尝试时console.log('MSG' + msg),它将打印出整个缓冲区,因此,看看是否有任何作用是没有用的。

如何正确处理这些数据?即使此数据不是面向行的,惰性模块也可以工作吗?是否有其他模块可以处理非面向行的流?


阅读 308

收藏
2020-07-07

共1个答案

小编典典

确实已经说过,还有很多工作要做,因为Node必须获取该缓冲区,然后将其推入v8
/将其转换为字符串。但是,在缓冲区上执行toString()并没有更好的效果。据我所知,目前还没有很好的解决方案,尤其是如果您的最终目标是找到一个字符串并胡闹的话。Ryan提到@nodeconf是需要完成工作的领域之一。

至于定界符,您可以选择任何您想要的。许多二进制协议选择包含固定的标头,这样您就可以将事物放到正常的结构中,而该结构通常包含一个长度。这样,您就可以分割一个已知的标头并获取有关其余数据的信息,而不必遍历整个缓冲区。使用这样的方案,可以使用以下工具:

顺便说一句,可以通过数组语法访问缓冲区,也可以使用.slice()将其分割。

最后,在这里检查:https :
//github.com/joyent/node/wiki/modules-找到一个解析简单tcp协议并且看起来做得很好的模块,并阅读一些代码。

2020-07-07