Rapsssito / react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with SSL/TLS support.
MIT License
315 stars 80 forks source link

How to handle file stream to byte stream? #60

Closed housan-eng closed 4 years ago

housan-eng commented 4 years ago

Sorry to disturb you again, I need to transfer files using TCP, how do I handle the file stream to byte stream

Rapsssito commented 4 years ago

@houzhanshan, a "file stream" is actually a byte stream. Just make sure to keep all data on a buffer and then dump it wherever you want:

const buffer = new Buffer(0);
socket.on('data', (chunk) => {
    buffer = Buffer.concat([buffer, chunk]);
});

socket.on('close', () => {
  // Do something with the buffer
});

You can find more info on this gist

housan-eng commented 4 years ago

Thanks a lot, help me