2

You can send DataView through WebRTC DataChannel. Source: https://groups.google.com/forum/#!topic/discuss-webrtc/JlU2ItCJuZU

This is great, because since then it isn't necessary to send always whole ArrayBuffer. But.. You can not dynamically change byteLength of DataView (and this is big surprise for me, because DataView is like a pointer to ArrayBuffer with just specialized API).

So still you can't send any length of bytes, without creating new DataView each time. (terrible idea because of GC).

Any ideas how to send any length of bytes through WebRTC, without creating new DataView each time?

ElSajko
  • 1,612
  • 3
  • 17
  • 37

2 Answers2

0

There is no way of doing that. If you want to send a message of x bytes then you have to have a DataView or ArrayBufferView or just an ArrayBuffer that is x bytes in length.

DataViews and ArrayBufferViews are very light weight. Creating two, three or even 20 DataViews is not a real challenge for GC. I cannot imagine a situation where one would need to create a lot of DataViews (for example in a tight loop) and then send them through the DataChannel.

However if you are creating a lot of DataViews just to modify the underlying ArrayBuffer before sending it, I would recommend using an ArrayBufferView (for example a Uint8Array or Uint32Array and so on) because it's easier to use. You can for example access the elements directly using the [] operator.

If you want to send a lot of small messages at once through the Data Channel then you should collect them in a big Buffer (using an ArrayBuffer or and ArrayBufferView) and then send them all at once. This has the additional advantage of sparing the data channel the need to handle all of these small messages itself. In other words the data channel will transfer the messages faster and the app will be more responsive.

Svetlin Mladenov
  • 4,307
  • 24
  • 31
  • The point is, it is about fast paced multiplayer game, where world has multiple objects and amount of this objects can be changed dynamically while gameplay. Host sends snapshot of objects every 50 or 100 ms and every snapshot may has 10, 58, 128, 500 or more or less object-data to send, so sometimes it will need 2000 bytes and sometimes 100, that is why it need flexible way. – ElSajko Jan 05 '15 at 11:32
  • 1
    Well if performance is really such an issue then the only thing that comes to my mind is to pre-allocate all the possible messages sizes that can be send (100, 200, 2000) if the message sizes are known in advance. However, as I said, creating and garbage collecting a DataView is not a major problem. But still there is now way to change the length of a DataView once it's been created. – Svetlin Mladenov Jan 05 '15 at 11:38
0

The simple answer is to use a UInt8Array (or what have you - some form of ArrayBufferView), and then after determining the size you want to send, use .slice(0,N) to create an object of the intended size.

Yes, it generates garbage (but so does a lot that JS does). It isn't large, since it's a shallow copy (reference instead of actual copy). If you're really insistent on avoiding garbage, create views of all the sizes you care about, and then select the one with size >= the size you want to send (and include a length value in the object, if it's not precisely the right size)

jesup
  • 6,765
  • 27
  • 32