Question

I have this code at nodejs server that is working with require('net'), so it is TCP, client side is AS3 and sending data correctly:

var server = net.createServer(function(socket) {

    socket.setEncoding("utf8");
    clients.push(socket);   

    function broadcast(message) {
        clients.forEach(function (client) {
            client.write(message);
        });
    }

    socket.on('data', function(dt){
        var rdt = dt;
        var srdt = rdt.toString();
        var ordt = JSON.parse(srdt);
        console.log(ordt);
        broadcast(ordt);
    }); 

    socket.on('end', function(){...});
})

It can not parse the data and give all kinds of errors. The thing I could understand that I get "buffer" from the client. But I need to keep sockets opened, so common solutions with looping buffer won't work for me.

Please help me to solve this.

BTW, client that stringifies the data, when receives this data back as it was sent, parses it correctly.

Was it helpful?

Solution 2

ValRus is correct; you need the substring(0, str.length-1) to remove the last char.

  let str = reading.toString('utf8');
  console.log( "String = %s", str );
  let obj = JSON.parse( str.substring(0, str.length-1) );


  let str2 = JSON.stringify(obj, null, 4); // Reverse conversion
  console.log("Obj = %s", str2);           // and output to inspect

This is the code I needed to pull JSON out of an MQTT message buffer.

(And this would have been sooooo much easier to debug in Java or C - good Lord is Javascript a pain!)

OTHER TIPS

You need to know, that the only thing that is passed through TCP sockets are buffers on both ends.

So if one end sends JSON encoded data the other ends get a buffer, in order to parse JSON data you simply do this

JSON.parse(buffer.toString('utf8'))

The end that sends data needs to use JSON.stringify before writing to socket.

I solved the problem.

There is 1 specific error occuring

undefined:1
{"chat":"hey","nickname":"nick_name"}
                                     ^
SyntaxError: Unexpected token
DEBUG: Program node app exited with code 8

Unexpected token at the end of data string, is some ghost symbol that is not a white space. trim() doesn't work, so to substring the last symbor works. This is AS3 symbol, so we have to keep it. First you save this symbol in the new variable. the you erase this symbol from the line. After that you can parse the string. work with it.

When you finish working with it, stringify the object, then add ghost symbol to the end and send over the socket. Without this symbol AS3 will not parse the data.

I don't know why is that, but that works for me.

const { StringDecoder } = require("string_decoder");
let header = "";
const decoder = new StringDecoder("utf8");
let str = decoder.write(chunk); // chunk is continuous stream of data coming from TCP IP channel
header += str;
header.replace(/s/, "");
var obj = JSON.parse(header);
console.log(obj);

While receiving data through tcp ip net npm package, we face trouble in parsing the continous stream of data. string_decoder is a npm package which can parse it into a json readable format. Also since the packets being received has a variable length so one can basically put a check on header.length to fetch only a complete set of object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top