🍃このブログは移転しました。
3秒後、自動的に移動します・・・。

WebRTCのDataChannelをunreliableモードで使う

Specによると、

An RTCDataChannel can be configured to operate in different reliability modes.

A reliable channel ensures that the data is delivered at the other peer through retransmissions.
An unreliable channel is configured to either limit the number of retransmissions ( maxRetransmits ) or set a time during which transmissions (including retransmissions) are allowed ( maxPacketLifeTime ).

These properties can not be used simultaneously and an attempt to do so will result in an error.
Not setting any of these properties results in a reliable channel.

というわけで、何も指定してない場合は、`reliable`モードになってる。

オプションを渡すだけ

const pc = new RTCPeerConnection();

// このどっちかが指定されてれば、`unreliable`になる
pc.createDataChannel('unreliable-dc', {
  maxRetransmits: 10,
  maxPacketLifeTime: 10,
});

WebRTC 1.0: Real-time Communication Between Browsers

どうやって確認するか

ChromeFirefoxだと、コードでわかる。

const pc = new RTCPeerConnection();

const dc = pc.createDataChannel('unreliable-dc', {
  maxRetransmits: 10,
});

dc.reliable; // false

ただしこの`reliable`はSpecにも載ってないので、ご利用は計画的に・・。
少なくともSafariには生えてなかったので、その場合はどうやって確認すればいいんやろね?

WebRTC 1.0: Real-time Communication Between Browsers

Chromeは罠あります

const pc = new RTCPeerConnection();
const dc = pc.createDataChannel('xxx', {
  maxRetransmits: null,
});
dc.reliable; // false in Chrome, true in Firefox

Chromeはなぜか「SpecにBooleanって書いてあっても、プロパティが渡るだけで有効化されちゃう」実装が多い気がする・・・。