@@ -2095,9 +2095,9 @@ following example datagrams are only sent if the transport is ready to send.
2095
2095
async function sendDatagrams(url, datagrams) {
2096
2096
const wt = new WebTransport(url);
2097
2097
const writer = wt.datagrams.writable.getWriter();
2098
- for (const datagram of datagrams) {
2098
+ for (const bytes of datagrams) {
2099
2099
await writer.ready;
2100
- writer.write(datagram ).catch(() => {});
2100
+ writer.write(bytes ).catch(() => {});
2101
2101
}
2102
2102
}
2103
2103
</pre>
@@ -2117,8 +2117,8 @@ async function sendFixedRate(url, createDatagram, ms = 100) {
2117
2117
const wt = new WebTransport(url);
2118
2118
await wt.ready;
2119
2119
const writer = wt.datagrams.writable.getWriter();
2120
- const datagram = createDatagram();
2121
- setInterval(() => writer.write(datagram ).catch(() => {}), ms);
2120
+ const bytes = createDatagram();
2121
+ setInterval(() => writer.write(bytes ).catch(() => {}), ms);
2122
2122
}
2123
2123
</pre>
2124
2124
@@ -2148,14 +2148,22 @@ Sending data as a one-way stream can be achieved by using the
2148
2148
{{WebTransport/createUnidirectionalStream}} function and the resulting stream's writer.
2149
2149
2150
2150
<pre class="example" highlight="js">
2151
- async function sendData(url, data) {
2151
+ async function sendData(url, ... data) {
2152
2152
const wt = new WebTransport(url);
2153
2153
const writable = await wt.createUnidirectionalStream();
2154
2154
const writer = writable.getWriter();
2155
- await writer.write(data);
2155
+ for (const bytes of data) {
2156
+ await writer.ready;
2157
+ writer.write(bytes).catch(() => {});
2158
+ }
2156
2159
await writer.close();
2157
2160
}
2158
2161
</pre>
2162
+ <div class="note">
2163
+ <p> The streams spec
2164
+ [discourages] (https://streams.spec.whatwg.org/#example-manual-write-dont-await)
2165
+ awaiting the promise from write().</p>
2166
+ </div>
2159
2167
2160
2168
Encoding can also be done through pipes from a {{ReadableStream}} , for example using
2161
2169
{{TextEncoderStream}} .
@@ -2182,11 +2190,11 @@ and then consuming each {{WebTransportReceiveStream}} by iterating over its chun
2182
2190
async function receiveData(url, processTheData) {
2183
2191
const wt = new WebTransport(url);
2184
2192
for await (const readable of wt.incomingUnidirectionalStreams) {
2185
- // consume streams individually, reporting per-stream errors
2193
+ // consume streams individually using IFFEs , reporting per-stream errors
2186
2194
((async () => {
2187
2195
try {
2188
- for await (const chunk of readable) {
2189
- processTheData(chunk );
2196
+ for await (const bytes of readable) {
2197
+ processTheData(bytes );
2190
2198
}
2191
2199
} catch (e) {
2192
2200
console.error(e);
@@ -2257,21 +2265,22 @@ connect.onclick = async () => {
2257
2265
2258
2266
sendData.onclick = async () => {
2259
2267
const form = document.forms.sending.elements;
2260
- const rawData = sending.data.value;
2261
- const data = new TextEncoder('utf-8' ).encode(rawData );
2268
+ const data = sending.data.value;
2269
+ const bytes = new TextEncoder('utf-8' ).encode(data );
2262
2270
try {
2263
2271
switch (form.sendtype.value) {
2264
2272
case 'datagram' : {
2265
- await datagramWriter.write(data);
2266
- addToEventLog(`Sent datagram: ${rawData}`);
2273
+ await datagramWriter.ready;
2274
+ datagramWriter.write(bytes).catch(() => {});
2275
+ addToEventLog(`Sent datagram: ${data}`);
2267
2276
break;
2268
2277
}
2269
2278
case 'unidi' : {
2270
2279
const writable = await wt.createUnidirectionalStream();
2271
2280
const writer = writable.getWriter();
2272
- await writer.write(data );
2281
+ writer.write(bytes).catch(() => {} );
2273
2282
await writer.close();
2274
- addToEventLog(`Sent a unidirectional stream with data: ${rawData }`);
2283
+ addToEventLog(`Sent a unidirectional stream with data: ${data }`);
2275
2284
break;
2276
2285
}
2277
2286
case 'bidi' : {
@@ -2280,9 +2289,9 @@ sendData.onclick = async () => {
2280
2289
readFromIncomingStream(duplexStream.readable, n);
2281
2290
2282
2291
const writer = duplexStream.writable.getWriter();
2283
- await writer.write(data );
2292
+ writer.write(bytes).catch(() => {} );
2284
2293
await writer.close();
2285
- addToEventLog(`Sent bidirectional stream #${n} with data: ${rawData }`);
2294
+ addToEventLog(`Sent bidirectional stream #${n} with data: ${data }`);
2286
2295
break;
2287
2296
}
2288
2297
}
@@ -2321,8 +2330,8 @@ async function acceptUnidirectionalStreams() {
2321
2330
async function readFromIncomingStream(readable, number) {
2322
2331
try {
2323
2332
const decoder = new TextDecoderStream('utf-8' );
2324
- for await (const chunk of readable.pipeThrough(decoder)) {
2325
- addToEventLog(`Received data on stream #${number}: ${chunk }`);
2333
+ for await (const data of readable.pipeThrough(decoder)) {
2334
+ addToEventLog(`Received data on stream #${number}: ${data }`);
2326
2335
}
2327
2336
addToEventLog(`Stream #${number} closed`);
2328
2337
} catch (e) {
0 commit comments