New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
not clear on how server can receive binary from client #4
Comments
Hi, The 'binary' event returns a readable stream, see https://github.com/sitegui/nodejs-websocket#event-binaryinstream That means you should write: // Listen for binary event
conn.on("binary", function (inStream) {
// Collect all the data in a buffer
var data = new Buffer(0)
// Get all frames of binary data and add to the buffer
inStream.on("readable", function () {
var newData = inStream.read()
if (newData)
data = Buffer.concat([data, newData], data.length+newData.length)
})
// Done, process the big data
inStream.on("end", function () {
process_my_data(data)
})
}) I haven't noticed an example of this use case is missing... I have designed this way because a binary data can be very long and the client can split it in different websocket frames. A security hint: put a limit in the Buffer above, because it is stored in the process memory and someone could send you tons of data and make node run out of memory. This would collapse your program entirely ;) (this module does this internally, see https://github.com/sitegui/nodejs-websocket#wssetmaxbufferlengthbytes) Hope it helps, |
Superb !!! works like a charm - merci beaucoup |
@sitegui, I'd add this to readme, since I'm working with JS every day, but not with Node.js and having need for very simple WebSockets server it was not clear for me how to get binary data as well. |
Hi @nazar-pc, that's a valid point. I'm open to a pull request if you like, otherwise I can manage to do this by the middle of the week. Cheers |
I would like to use your library to allow my browser client side to send to node-js server side binary data in form of a typed array such as Uint8Array. I am successfully sending normal text from client to server. When attemping binary, here is my client side send
socket.binaryType = 'arraybuffer';
socket.send(big_monster_data);
and here is my nodejs-websocket server side which is NOT correctly receiving the client sent data
var server = ws.createServer(function (connection_request) {
}
thank you for your time
cheers, Scott Stensland
The text was updated successfully, but these errors were encountered: