0

I have some data in a .glb file that starts off like this:

glTF ¤Ö @ JSON

I read the file using this code:

fs.readFile(file,  (err, data) => {

    if (err) console.log(err);

    var message = {
        "Message": "glb_string",
        "Item": data
    }
    ws.send(JSON.stringify(message));
});

And send it off using a websocket connection to a client websocket server.

However, the Node buffer returned by fs.readFile looks like this:

Buffer@0x0000020A2BF455D0 67 6c 54 46 02 00 00 00 a4 d6 01 00 40 08 00 00 4a 53 4f 4e 7b ... 

These values are incorrect. It gets simple letters correct in utf-8 form:

g(67) l(6c) T(54) F(46)

But once it gets to special characters like ¤ and Ö the buffer returns 00. The buffer should look like this:

Buffer@0x000001DD07B0E590 67 6C 54 46 02 20 20 20 C2 A4 C3 ... 

or in decimal form:

103 108 84 70 2 32 32 32 164 214 1 32 64 8 32 32 74 83 79 78

2 is "stx start of text ctrl-b"
1 is "soh start of heading ctrl-a"
164 is ¤ 
214 is Ö

Not sure why the raw buffer that is being returned does not use the right values for those special characters. As a result the .glb file is not usable.

Thank you for any help.

  • might be a dumb question, but have you checked your file with a hex editor? i just tried it using your sample string, and i get exactly the same results when i view the hex of the file via `xxd` (linux command line) as with `fs.readFile` – David784 Jul 11 '18 at 21:50
  • I haven't tried that, what I did was put the string into this website: https://r12a.github.io/app-conversion/ – Callum Thomas Jul 11 '18 at 22:06
  • The real reason behind all this is that I'm trying to transfer a .glb file, which is a binary gltf file, across a websocket connection intact. To do that, I have to read the .glb file, send it via JSON.stringify, then decode it on the client-side. So far I haven't been able to encode the .glb server side, send it to the client via websocket, then decode it client-side and keep the .glb file working. The .glb file that results on the client is not usable by gltf viewers. – Callum Thomas Jul 11 '18 at 22:09
  • Hmm, you are right that the hex editor looks the exact same as fs.readFile. I think the reason my code isn't working is because the .glb file specification requires a 12 byte header, which the converter isn't accounting for. – Callum Thomas Jul 11 '18 at 22:20

0 Answers0