JSON for websocket gives error

Asked

Viewed 47 times

1

I send a JSON to the websocket. JSON and sending is set 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 file name (each file has N json lines), its sequence (as the same file has N jsons, we need to rebuild in the correct order) and date, which contain the data obtained from the webcam (video+audio).

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

What might it be? in the backend handle so 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);
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.