mi6crazyheart mi6crazyheart - 1 year ago 132
Javascript Question

How to use flatbuffer generated data in browser?

I'm trying to use flatbuffer in one of my web application. I've already stored those buffer data in one file (buffer_content.txt) by using following php code.

// ...Code to store to disk or send over a network goes here...
$file = 'buffer_content.txt';
$output = serialize($builder->dataBuffer());

$fp = fopen($file, "w");
fwrite($fp, $output);

Through ajax I can get the buffer data from the server. Now I need to extract the original data from that buffer in JavaScript. But, I can't able to fig out how to do that.

Any idea, how to do this ?

Answer Source

You don't want to use serialize. the dataBuffer already contains serialized data, check out what it says here: https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html

$buf = $builder->dataBuffer(); // Of type Google\FlatBuffers\ByteBuffer

// The data in this ByteBuffer does NOT start at 0, but at buf->getPosition().

// The end of the data is marked by buf->capacity(), so the size is

// buf->capacity() - buf->getPosition().

Make sure you write the file in binary mode (pass "wb" to fopen). Also don't call it .txt since it isn't a text format :)

Then in JS, you read in the file (again, in binary mode, not text), make sure it ends up in a Uint8Array, then follow the code here: https://google.github.io/flatbuffers/flatbuffers_guide_use_javascript.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download