Sharing binary buffer between Node.js server and Browser

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 10:36:23

问题


There is an issue of how to share buffers between node.js and the browser containing binary data. I'm pretty happy with Socket.io as a transport layer but the issue is that there is no porting of the Buffer class for the browser. Not something I can find anyways

I've also came across binary.js and I was wondering if there is a good way to combine them having the socket.io as the transport layer and the Binary.js as the data medium. I also saw this question, which is kind of on topic but doesn't really resolve the issue.

I know socket.io added binary support but I haven't found any documentation on the topic.

Update:

It seems that binary.js will not be the solution. The basic requirement that I want is to share the same capabilities that Buffer has in node with the browser.

My needs consist of two things:

  1. Handle the buffer in the same manner in both Server and Browser.

  2. support Binary data.

I will probably use Array Buffer.

Edit: Since node.js run over V8 you can use ArrayBuffer. It seems as if the issue is solved. Yet, from what I know, node people decided that it's a good idea to create a buffer module and manage it in the C bindings they created (from a talk given by Ryan Dahl). I think this has to do with how buffering is done over the network. This means ArrayBuffer is still not a good data medium to share between server and browser.


回答1:


browser-buffer emulates Node's Buffer API in the browser.

It's backed by a Uint8Array, so browser support is sketchy.




回答2:


JavaScript's built in strings use wide characters internally. So they can easily store a value from 0 to 255 in each character position. This is a JavaScript language feature, so it should work the same in a browser or in node.js.

You can use charCodeAt to extract the value of a particular position in a string and fromCharCode to create a character (that you can add to a string) with a value from 0 to 255.

You can use the various string functions to manipulate data in this form. You can create constants using JavaScript string constants like this "\x00\x12\x34\x56".



来源:https://stackoverflow.com/questions/12229079/sharing-binary-buffer-between-node-js-server-and-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!