WebTransport over QUIC 这个规范介绍WebTransport over QUIC的实现规范, 目前版本(M89)的WebTransport实现是基于QUIC的。
WebTransport over HTTP/3 这个规范介绍WebTransport over HTTP/3的实现规范,目前版本(M89)还没有实现这这部分。 按照官方的说法是,WebTransport 最终会完全基于HTTP/3 实现,并移除基于QUIC的实现,这个还需要进一步关注。
在浏览器中可以通过URL 来实例化一个WebTransport 实例,比如:
consturl=‘quic-transport://example.com:4999/foo/bar';
consttransport=newWebTransport(url);// Once .ready fulfills, the connection can be used.
awaittransport.ready;
创建双向通信的通道:
constrs=transport.receiveBidrectionalStreams();constreader=rs.getReader();while(true){const{done,value}=awaitreader.read();if(done){break;}// value is a BidirectionalStream
// value.readable is a ReadableStream
// value.writable is a WritableStream
}
创建单向通信的通道:
// Send two Uint8Arrays to the server.
conststream=awaittransport.createSendStream();constwriter=stream.writable.getWriter();constdata1=newUint8Array([65,66,67]);constdata2=newUint8Array([68,69,70]);writer.write(data1);writer.write(data2);// Receive data from the server
constrs=transport.receiveStreams();constreader=rs.getReader();while(true){const{done,value}=awaitreader.read();if(done){break;}// todo
}
创建udp数据通道:
// Send two datagrams to the server.
constws=transport.sendDatagrams();constwriter=ws.getWriter();constdata1=newUint8Array([65,66,67]);constdata2=newUint8Array([68,69,70]);writer.write(data1);writer.write(data2);// Read datagrams from the server.
constrs=transport.receiveDatagrams();constreader=rs.getReader();while(true){const{value,done}=awaitreader.read();if(done){break;}// value is a Uint8Array.
console.log(value);}