Git Product home page Git Product logo

Comments (2)

thelsing avatar thelsing commented on August 24, 2024

I went with

            ByteBuffer buffer = ByteBuffer.allocate(message.length + Integer.BYTES);
            buffer.putInt(message.length).put(message).rewind();

            int chunkSize = 16 * 1024;

            while (buffer.remaining() > 0) {
              var amountToSend = buffer.remaining() <= chunkSize ? buffer.remaining() : chunkSize;
              ByteBuffer part = buffer;

              if (amountToSend != buffer.capacity()) {
                // we need to allocation a new ByteBuffer because send calls ByteBuffer.array()
                // which would return
                // the whole byte[] and not only the slice. But the lib doesn't use
                // ByteBuffer.arrayOffset().
                var slice = buffer.slice(buffer.position(), amountToSend);
                part = ByteBuffer.allocate(amountToSend);
                part.put(slice);
              }

              buffer.position(buffer.position() + amountToSend);
              localDataChannel.send(new RTCDataChannelBuffer(part, true));
              log.debug(prefix() + " sent " + part.capacity() + " bytes");
            }

for sending and

private ByteBuffer messageBuffer = null;
public final byte[] readMessage(ByteBuffer part) {
    if (messageBuffer == null) {
      int length = part.getInt();
      notifyListeners(Direction.Inbound, State.Start, length, 0);

      if (part.remaining() == length) {
        var ret = new byte[length];
        part.get(ret);
        notifyListeners(Direction.Inbound, State.Complete, length, length);
        return ret;
      }

      messageBuffer = ByteBuffer.allocate(length);
    }

    messageBuffer.put(part);
    notifyListeners(
        Direction.Inbound, State.Progress, messageBuffer.capacity(), messageBuffer.position());

    if (messageBuffer.capacity() == messageBuffer.position()) {
      notifyListeners(
          Direction.Inbound, State.Complete, messageBuffer.capacity(), messageBuffer.capacity());
      var ret = messageBuffer.array();
      messageBuffer = null;
      return ret;
    }

    return null;
  }

plus null checking for receiving.

from webrtc-java.

stserakhau avatar stserakhau commented on August 24, 2024

look to this article https://stackoverflow.com/questions/35381237/webrtc-data-channel-max-data-size

plus you should understand that it's UDP and part of the data may lost in network, I saw this case on transmitting files great than 500kb on slow network connection (at the moment not sure is it bug of the library or success part of data transmission via webrtc technology)

from webrtc-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.