-1

I have two problems:

1. When I send text or binary larger than 128 kb in Chrome, Chrome sends fragmented websocket frames. These frames should end with the last frame and fin=1. But Chrome is also sending Frames with unknown opcodes.

2. When I try to send large files in Firefox, then Firefox sends the data very slow in my home network(~300kb/s and slows down while sending TCP packages) and the file is never complete. Also the frame for the binary file is missing. I tracked the network with Wireshark. Safari chrashes by the same try.

I'm using Firefox 43.0.4 for Mac and Chrome 48.0.2564.97 for Mac Safari 9.0.3 And I'm using my own server in Node.js

UPDATE I found the problem but don't know the solution.

 socket.on('data', function(data) {
    buffer = Buffer.concat([buffer, data]);
    _editBuffer();
});

Buffer.concat slows the transfer down. But how can I use the + operator for Buffer concatenation ?

benu
  • 59
  • 1
  • 12
  • If you constantly append to a buffer that needs to be re-allocated each time (maybe the case here, not sure), you're going to have O(N^2) performance. The code would need to be modified to avoid actually joining the set of data buffers until finally necessary, which may require reworking some of the surrounding logic. You cannot use the `+` operator to join arrays/buffers (as mentioned [in this answer](http://stackoverflow.com/a/7124918/1114), it's only capable of adding numbers and joining strings). – Jeremy Feb 26 '16 at 00:55
  • How can this be done? I only have the 'socket on data' event and have to store the incoming chunks, if its only one websocket frame like in firefox. It seems that chunking big files on client side is the best method, so than the file can be written in peaces. Because if I use it in one websocket frame, I have to store all in one buffer, to unmask the payload. Is that right ? – benu Feb 29 '16 at 23:17

1 Answers1

-1

I found a solution. Every time a websocket frame from a binary file comes to the server, this frame has to be written with fs.createWriteStream in append mode. After that the Buffer has to be sliced from the written frame.

 var stream  = fs.createWriteStream(__dirname + '/' + fileInfo.fileName, {flags:'a'});
...
stream.write(payload);
benu
  • 59
  • 1
  • 12