TransWikia.com

io.socket restarts when sending large files from Android

Stack Overflow Asked by M. Usman Khan on September 2, 2020

I am using this library

api('io.socket:socket.io-client:1.0.0') {
        exclude group: 'org.json', module: 'json'
    }

I am sending files using the code below. It works for small files up to about 6mb. But for larger files, like 30mb, it attempts to upload for about 5 seconds but then the socket automatically "reconnects" without uploading.

        input = file.inputStream()
        byteArray = input.readBytes()

        val fileJson = JSONObject()
        fileJson.put("file", byteArray)
        socket.emit("media/upload", fileJson)

The server end seems fine, because it works from web to web.

Am I not doing it right?

One Answer

I had the same issue when dealing with files (100Mb), I then split the file into several small chunks/packets (1024 bytes), then make sure you don't create a new connection for every packet, you can then you can always write the packets to disk to get the file.

for splitting file you can reference How to break a file into pieces using Java?

It also has a sample on merging the file back

This is not the best solution but will get you going.

Answered by ishm on September 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP