Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js net server数据事件对象是缓冲区吗?_Node.js_Sockets_Tcp_Buffer_Server - Fatal编程技术网

node.js net server数据事件对象是缓冲区吗?

node.js net server数据事件对象是缓冲区吗?,node.js,sockets,tcp,buffer,server,Node.js,Sockets,Tcp,Buffer,Server,我试图在node.js中创建一个tcp游戏服务器,但在数据传输方面遇到了问题。我想在sockets DATA事件中逻辑地获取我接收的缓冲区的长度(字节),假设对象是缓冲区(根据我所读的内容),我尝试了以下方法: var server = net.createServer(); server.on("connection", function(socket) { socket.on("data", function(data) { console.log(data.len

我试图在node.js中创建一个tcp游戏服务器,但在数据传输方面遇到了问题。我想在sockets DATA事件中逻辑地获取我接收的缓冲区的长度(字节),假设对象是缓冲区(根据我所读的内容),我尝试了以下方法:

var server = net.createServer();
server.on("connection", function(socket) {

    socket.on("data", function(data) {
        console.log(data.length);
    });

});
但很明显,它返回的是未定义的。这让我非常困惑,因此我尝试通过以下操作查看我是否在处理缓冲区:

var server = net.createServer();
server.on("connection", function(socket) {

    socket.on("data", function(data) {
        console.log(Buffer.isBuffer(data));
    });

});
我是假的!谁能解释一下我做错了什么?
另外,我在套接字上没有设置编码

此代码很好。我在别的地方搞砸了

你的代码片段对我来说很好。您的代码(或者您正在使用的模块中的代码)中一定有其他东西正在调用
socket.setEncoding()
或类似程序。