ket
ket

Reputation: 995

How to separate JSON data from ArrayBuffer receiving from websocket

I tried unsuccessful to separate JSON data from an array of buffer receiving from websocket which look like this.

{"type":"string","data":{"UklGRkIjAABXRUJQVlA4IDYjAACQswCdASqrAfAAPm0wlUemI"}}
[object ArrayBuffer]

the objective is to read them separately with proper function. My old method is parse the JSON first. If fail, pass through another function.

The reason I send them in both formats is that converting JSON data to an array or array to JSON would increase file size around three fold. The best practice is to send them separately. However, at the terminal I read JSON data with

var json = JSON.parse(e.data);

and read the ArrayBuffer with DataView method.

The app works properly on the surface, but if you check at console.log there are too many Uncaugth Error.

It also blocks data flow at one point, causing the stream overflow not very smooth.

Thanks for any suggestions in advance.

Upvotes: 0

Views: 1198

Answers (1)

ket
ket

Reputation: 995

Got it:

if ((typeof data == 'object')) {
   // this would return ArrayBuffer
} else {
   // this would return JSON
}

Upvotes: 0

Related Questions