nkzawa / socket.io-stream

Stream for Socket.IO
MIT License
611 stars 111 forks source link

chunk.toArrayBuffer is not a function #87

Closed rico345100 closed 7 years ago

rico345100 commented 8 years ago

When I'm sending file, always fails with:

socket.io-stream.js:1193 chunk.toArrayBuffer is not a function

message from client(latest chrome browser) and this is my client src:

var stat = $('#stat');

$('#file').change(function(e) {
    var file = e.target.files[0];
    var stream = ss.createStream();

    ss(socket).emit('upload', stream, {
        name: file.name,
        size: file.size
    });

    var blobStream = ss.createBlobReadStream(file);
    var size = 0;
    blobStream.on('data', (chunk) => {
        size += chunk.length;
        stat.text('Uploading ' + size + ' / ' + file.size);
    });
    blobStream.pipe(stream);
});

and this is server code:

    ss(socket).on('upload', (stream, data) => {
        var filename = path.basename(data.name);
        stream.pipe(fs.createWriteStream(filename));
    });

I'm using node -v 6.2.2. What is the problem?

\ note: I used browserify to move socket.io-stream.js file into client. Why I told this because I saw the comment: // socket.io can't handle Buffer when using browserify. chunk = chunk.toArrayBuffer();

rico345100 commented 8 years ago

guys, I load the script directly without bundling by browserify, it works. But something another happened. Look at this server side code:

    ss(socket).on('/user/update/profile', (stream, data) => {
        const filename = path.basename(data.name);
        const ws = fs.createWriteStream(`userdata/profile/${filename}`);

        stream.on('error', (e) => {
            console.log('Error found:');
            console.log(e);
        });
        stream.on('drain', (e) => {
            console.log('drain');
        });
        stream.on('data', () => {
            console.log('data');
        });
        stream.on('close', () => {
            console.log('close');
        });
        stream.pipe(ws);
    });

When I was sent file from browser with socket.io-stream, it works. But when I tried with Electron, it won't work with this error message:

TypeError: Invalid non-string/buffer chunk

I saw similar issue, not Electron, nw.js. He found some solution but it didn't worked for me. I logged every data sending from client on Electron's BrowserWindow(console of inspector I mean), looks like it sent well:

        var file = ev.target.files[0];
        var stream = ss.createStream();

        ss(socket).emit('/user/update/profile', stream, {
            email: this.props.user.email,
            name: file.name,
            size: file.size
        });

        var blobStream = ss.createBlobReadStream(file);
        var size = 0;
        blobStream.on('data', (chunk) => {
            size += chunk.length;
            console.log(`${size} / ${file.size}`);
        });
        blobStream.pipe(stream);

and this is what console logged:

    16384 / 4514096
    app.js:63105 32768 / 4514096
    app.js:63105 49152 / 4514096
    app.js:63105 65536 / 4514096
    ...
    app.js:63105 4505600 / 4514096
    app.js:63105 4514096 / 4514096

Client sent the data without any error, but server failed to create file with error. What should I do?

techyaura commented 8 years ago

I have used the file directly, by placing it in some of my folder, it also worked for me.

But if am using browserify, it gives me error chunk.toArrayBuffer is not a function

calbertts commented 8 years ago

I'm having the same issue but I'm using JSPM.

Any idea?

calbertts commented 8 years ago

Guys, I was able to solve this by using:

socketStream.forceBase64 = true

Regards.

Gottox commented 8 years ago

Got the same issue with node-webterm which uses terminal.js and browserify.

colwynmyself commented 8 years ago

Just by the way you guys, this is because Browserify handles chunks differently. I have a PR open to fix this that hasn't been merged: https://github.com/nkzawa/socket.io-stream/pull/86

If you want I have a fork here that has the single commit. It's what I've been using at my company. That's here: https://github.com/colwynmyself/socket.io-stream

nkzawa commented 7 years ago

Fixed!