Giter Site home page Giter Site logo

Comments (3)

lbaozi avatar lbaozi commented on July 26, 2024 1

我也遇到这个问题,打开了peerjs的debug看到是由于包太大导致的,对于大页面初始包会比较大,可以简单的自己实现一个peerjs的传输在发送时将发送的数据分片,接收的时候重新拼装

这个问题你解决了吗?

解决了,因为我只是做一个demo所以在发送的时候将数据进行了简单的拆分

async send<T>(data: T) {
  if (!this.conn) {
    await this.connect();
  }
  while (this.role === 'embed' && !this.opened) {
    // a spin lock to wait connection open
    await sleep(50);
  }
  let dataStr = JSON.stringify(data);
  const MAX_LENGTH = 200000;
  if (dataStr.length > MAX_LENGTH) {
    const count = Math.ceil(dataStr.length / MAX_LENGTH);
    for (let i = 0; i < count; i++) {
      const sendData = dataStr.slice(0, Math.min(dataStr.length, MAX_LENGTH));
      dataStr = dataStr.slice(Math.min(dataStr.length, MAX_LENGTH));

      this.conn?.send(`part${i + 1}-${count}endpart;${sendData}`);
    }
  } else {
    this.conn?.send(data);
  }
}

然后在接收的地方重新组装

conn.on('data', data => {
  data = this.joinData(data);
  if (!data) return;
  const { event, payload } = data;
  this.handlers[event as TransporterEvents].map(h =>
    h({
      event: event,
      payload: payload,
    })
  );
});
joinData (data:any) {
  if (typeof data === 'string' && data.startsWith('part') && data.includes('endpart;')) {
    const partData = data.split('endpart;');
    const index = parseInt(partData[0].split('-')[0].replace('part', ''), 10);
    const count = parseInt(partData[0].split('-')[1], 10);
    this.temp[index - 1] = partData[1];
    this.tempLength = this.tempLength + 1;
    console.log(`收到了拆包,当前包${index},共计:${count},已接收:${this.temp.length}`)

    if (this.tempLength !== count) return
      data = JSON.parse(this.temp.join(''));
      this.temp = [];
      this.tempLength = 0;
  }
  return data;
}

from syncit.

lbaozi avatar lbaozi commented on July 26, 2024

我也遇到这个问题,打开了peerjs的debug看到是由于包太大导致的,对于大页面初始包会比较大,可以简单的自己实现一个peerjs的传输在发送时将发送的数据分片,接收的时候重新拼装

from syncit.

TOmANDJerryz avatar TOmANDJerryz commented on July 26, 2024

我也遇到这个问题,打开了peerjs的debug看到是由于包太大导致的,对于大页面初始包会比较大,可以简单的自己实现一个peerjs的传输在发送时将发送的数据分片,接收的时候重新拼装

这个问题你解决了吗?

from syncit.

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.