JSON for websocket gives error

1

I send a JSON to the websocket. JSON and sending is defined like this:

var binary = btoa(Uint8ToString(view));

var toSend = {

  'fileName': fileName,
  'sequence': SendedChunks,
  'data': binary.toString()

};


var z = JSON.stringify(toSend);

socket.send(z);

In the parameters we have the filename (each file has N json lines), its sequence (as the same file has N jsons, we need to reconstruct in the correct order) and date, which contain the data obtained from the webcam + audio).

This part works fine, but when it arrives at the backend json is not decoded as it should be ... sometimes the string with json does not close (there is no key at the end) others do not seem to come open with the braces and parameters, comes only the final part) ... I do not know what to do since some jsons are processed correctly, but after sending a few times it starts to ruin the json.

What could it be? in the backend I treat the json sent by the code I wrote earlier:

byte[] payloadData = receiveBuffer.Array.Where(b => b != 0).ToArray();

//Because we know that is a string, we convert it. 
string receiveString = System.Text.Encoding.UTF8.GetString(payloadData, 0, payloadData.Length);
dynamic receivedObj = Json.Decode < dynamic > (receiveString); //AQUI DÁ ERRO
TransFile transFileChunk = new TransFile();

string Key = receivedObj["fileName"];
transFileChunk.sequence = receivedObj["sequence"];

string ba = receivedObj["data"];
transFileChunk.bytes = Convert.FromBase64String(ba);
    
asked by anonymous 30.09.2017 / 01:51

0 answers