RTCPeerConnection
extension and looks like this: const dc = pc.createDataChannel("some label string"); // , – , – // send dc.send("some string"); // otherPc.addEventListener('datachannel', e => { const channel = e.channel; channel.onmessage = event => { console.log('received', event.data); }); });
google-chrome-unstable --enable-blink-features=RTCQuicTransport,RTCIceTransportExtension
const ice1 = new RTCIceTransport(); ice1.onstatechange = function() { console.log('ICE transport 1 state change', ice1.state); }; const ice2 = new RTCIceTransport(); ice2.onstatechange = function() { console.log('ICE transport 2 state change', ice2.state); }; // ICE- ice1.onicecandidate = function(evt) { console.log('1 -> 2', evt.candidate); if (evt.candidate) { ice2.addRemoteCandidate(evt.candidate); } }; ice2.onicecandidate = function(evt) { console.log('2 -> 1', evt.candidate); if (evt.candidate) { ice1.addRemoteCandidate(evt.candidate); } }; // ICE- ice1.start(ice2.getLocalParameters(), 'controlling'); ice2.start(ice1.getLocalParameters(), 'controlled'); ice1.gather(iceOptions); ice2.gather(iceOptions);
const quic1 = new RTCQuicTransport(ice1); quic1.onstatechange = function() { console.log('QUIC transport 1 state change', quic1.state); }; const quic2 = new RTCQuicTransport(ice2); quic2.onstatechange = function() { console.log('QUIC transport 2 state change', quic2.state); }; // QUIC quic2.addEventListener('quicstream', (e) => { console.log('QUIC transport 2 got a stream', e.stream); receiveStream = e.stream; });
The RTCQuicTransport connection is configured with an API public key. At the moment we are not planning for this API to replace the original check. It will be replaced by an alarm that identifies deleted certificates to validate self-signed certificates - when QUIC starts supporting it in Chromium.So far so good.
sendStream
only after the QUIC transport goes to the “connected” state - in a different state this would result in an error: quic1.onstatechange = function() { console.log('QUIC transport 1 state change', quic1.state); if (quic1.state === 'connected' && !sendStream) { sendStream = quic1.createStream('webrtchacks'); // createDataChannel. document.getElementById('sendButton').disabled = false; document.getElementById('dataChannelSend').disabled = false; } };
document.getElementById('sendButton').onclick = () => { const rawData = document.getElementById('dataChannelSend').value; document.getElementById('dataChannelSend').value = ''; // Uint8Array. , TextEncoder. const data = encoder.encode(rawData); sendStream.write({ data, }); };
onquicstream
event on a remote QUIC transport: // QUIC quic2.addEventListener('quicstream', (e) => { console.log('QUIC transport 2 got a stream', e.stream); receiveStream = e.stream; receiveStream.waitForReadable(1) .then(ondata); });
function ondata() { const buffer = new Uint8Array(receiveStream.readBufferedAmount); const res = receiveStream.readInto(buffer); const data = decoder.decode(buffer); document.getElementById('dataChannelReceive').value = data; receiveStream.waitForReadable(1) .then(ondata); }
receiveStream
will be read, decoded into text and placed in the output field. And so every time there will be data available for reading.For a long time, our SDKs use web socket for signaling. This is an excellent, time-tested standard, but there are some problems with it. First is TCP. And TCP is not so good and fast on mobile networks, plus it does not support roaming between networks. Secondly, it is often text (there is also a binary mode, but you will rarely see it).
We recently launched a closed beta test of the signal protocol on the DataChannel. This protocol is also not without drawbacks, but since it works in bad networks and when roaming, it conquers at a glance. Changed the network? No need to re-connect.ICE Restart
in most cases will help find a new path for traffic. But, as I said, the protocol still has flaws: not all browsers support all protocol extensions, such as guaranteed delivery and support for packet order; Also, the protocol does not support gzip for text mode out of the box. But all these problems can be solved on the application side.
Source: https://habr.com/ru/post/441168/
All Articles