1
votes

I'm attempting to make a tcp game server in node.js but am having problems with data transmission. I want to get the length (in bytes) of the buffer i receive in the sockets DATA event so logically, under the assumption that the object is a buffer (from what i've read) i tried this :

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

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

});

But apparently it returns undefined. This confused me quite alot so i tried to see if i'm even handling a buffer here by doing this :

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

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

});

And i got false! Could anybody explain what i'm doing wrong? ps. i have no encoding set on the socket

1
Your code snippets work just fine for me. There must be something else in your code (or perhaps code from a module that you're using) that is calling socket.setEncoding() or similar. - mscdex

1 Answers

-1
votes

This code is fine. I messed up somewhere else!