Json output strange characters

357 Views Asked by At

I implemented socket in nodejs which get data from device in json format. But when i print these data it shows strange characters. I tested sending json in packet sender and it works when i am parcing this json. But it doesn't work with R51-GPRS device which sends data in json format UTF8 encoded.

Pic : Output of data

Text 1: ��_�9�d�Z�{��m�^�3�J�=�▒▒G�p�/��-� �3�Z�j�W�%�]�)�[����-�▒▒=�\�v�_�=�B�r�]�s�X�z��k�J�/��j�\�l�C�=� �/��y�J�e�▒▒,� �3�Z�m�J�e�▒▒,� �3�I�{�P�z��/� �=�V�l�C�=� �.� �3�L�z�L�z�▒▒.��j�\�y�▒▒.��j�\�|�K�=�=�J�{�N�=� �=�J�{�V�=��3�L�z�W�h�V�=��3�_�~�^�=�▒▒w�P�,� �3�_�m�N�m�▒▒=�q�/��&��,��k�T�=�▒▒/�/��&�%��,�▒▒=�X�=�▒▒/� �2��/��&��=�D

Pic 2 : Output of data

Text 2: �}LV�# LJ�e,C�|TI�iX�(X�gHJ�tWJ�hJC�tB@�|@K�gVX�$X�gJJ�uKW�|WJ�h?C�}8X�8��_�Te�v�v(G�1uG�va �n0?�[T�fW�d"G�vq�'|G�vv�lpU�d$R�a#V�20I�0w�:t �niG�;v�:�v(G�2aV�v>G�'w�=h�n!U�d>G�$a �10_�d"U�vq�0a �10_�d"U�vb�'{�v(V�d"I�8}�=h�n V� Q�vg�0g�&0_�g>G�'w�$0_�g>G�'w�5�n#I�!a�$e�n I�!a�8}�n]�vg�0|�8}�nS�vt�8u �n0�6{ �z"G�vt �9e�10_�ZS�d23�z+K�v>G��n0W�f#H�a?U�t#]�f+_�c0I�9s�n0U�y"T�+H�g? �y'�)o��_��"��A������E���Q������k���������A���L���F���@�������ύ�ߏG���D���Y���F���C������Q������G���X���������Q���������A���Q���������R���K�������߇M���X���������W���W���������G���������G���P����ߞQ���U����ߞQ���M������W���L���M������D���E������K������D���U����߿j������▒▒�����O���������������▒▒���߆C�������Ъ▒▒������ߖ_��_Y�"4�~=4�1+s�1u4�}{,�IX�"i$�!k&�1u4�c,e�1c4�uo.�#l&�$l#� kp�?{r�e0x�|{,�14y�v5x�~<4�1-p� i4�1,e�a�v{,�#i&�1?f�z#s�)j&�#u4�r+r�z#s�)j&�#u4�d=e�i<4� i&�?{z�t�v{,� h"�'u4�<r�<d�)h%�1,e�w?f�)

Text3: ��_��RDz�1���h廯5���!���p���▒▒��j���b���1���<���6���0���d���c���p��7���4���)夥6���3���h彬!���~弹7���(���a���~寺!���p���b���1���!���p���b���"���;���h���b��=���(���������'���'���p���~弹7���p���~弹7��� ���c��!���%���`��!���=���j���'���<���=���j���4���5���p���;���b���4���%���p�����r���k���~彣?���p���c�����c���g���p��3���p���b��k�����j��/��_�H�o�j��j�M�-�M�j��r�5��^�z�]�x�M�j��;�M�j� �p�_�x�X�}�\�.�C�,��&��r�M�'� �&��j�M�.�\�j�M�;��!� �r�_�x�M�8��-�U�x�_�j��,��-�U�x�_�j��;��j�\�x�C�$�!� �r�\�|�[�j��,��:�U�{�M�;� �8�U�{�M�;� �)� �r�C�=� �8� �r�C�=� �$�r�W�j��,� �$�

There is even chinese characters (it is chinese probably)

My code:

var net = require('net');
const { match } = require('assert');
var sockets = [];

net.createServer(function (socket) {
  console.log('socket connected');
  var buffer = '';
  socket.on('data', function(data) {
    if( !sockets.includes(socket)){
      var line = data.toString();
      console.log('got "data"', line);
      var rePattern = new RegExp(/Sec-WebSocket-Key:\s*(.*?)\r\n/);
      var key = line.match(rePattern);
      console.log("Key1" + key[1]);
  
      const buffer = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "+ key[1] +"\r\n\r\n";
      socket.write(buffer);
      sockets.push(socket); 
    }else{
      if (data) {
          console.log(data);
          var JSONdat = JSON.parse(data);
          console.log(JSONdat);
        }
    }
  });

  socket.on('end', function() {
    console.log('end');
  });
  socket.on('close', function() {
    console.log('close');
  });
  socket.on('error', function(e) {
    console.log('error ', e);
  });
}).listen(7788, function() {
  console.log('TCP Server is listening on port 7788');
});

Like i said it gets JSON from R51-GPRS device(documentation says it sends in json format utf8 encoded). I tested my code with another json it works but i don't know what this device actually send to socket. There is php code in documentation i imitated it but i stuck in here because i can't read this data. It has to be JSON.

2

There are 2 best solutions below

0
Ani On

I couldn't find solution in Nodejs. I tried to convert some php code to nodejs but it didn't worked. After many attempts used Ratchet and it works.

1
Someone Special On

When you use socket.on('data'), you are receiving a buffer. You need to wait for the data to be complete before you use it.

For example

let received = ""
client.on("data", data => {
  received += data
  const messages = received.split("\n") 
  if (messages.length > 1) {
    for (let message of messages) {
      if (message !== "") {
        console.log(JSON.parse(message))
        received = ""
      }
    }
  }
})