PresentationConnection_send_receiving-ua.html (1613B)
1 <!DOCTYPE html> 2 3 <meta charset="utf-8"> 4 <title>Sending a message through PresentationConnection</title> 5 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 6 <link rel="help" href="http://w3c.github.io/presentation-api/#sending-a-message-through-presentationconnection"> 7 <script src="../common.js"></script> 8 <script src="stash.js"></script> 9 10 <script> 11 const stash = new Stash(stashIds.toReceiver, stashIds.toController); 12 13 navigator.presentation.receiver.connectionList.then(list => { 14 const connection = list.connections[0]; 15 16 const message1 = '1st'; 17 const message2 = '2nd'; 18 const message3 = new Uint8Array([51, 114, 100]); // "3rd" 19 const message4 = new Uint8Array([52, 116, 104]); // "4th" 20 const message5 = new Uint8Array([108, 97, 115, 116]); // "last" 21 22 // send messages 23 return stash.receive().then(() => { 24 connection.send(message1); // string 25 connection.send(message2); // string 26 connection.send(new Blob([message3])); // Blob 27 connection.send(message4.buffer); // ArrayBuffer 28 connection.send(message5); // ArrayBufferView 29 30 return stash.receive(); 31 }).then(() => { 32 // try to send a message in the "closed" state 33 // Note: a receiving user agent does not fire a "terminate" event 34 connection.close(); 35 connection.onclose = () => { 36 try { 37 connection.send(message1); 38 stash.send(JSON.stringify({ type: 'ok' })); 39 } catch (e) { 40 stash.send(JSON.stringify({ type: 'error', name: e.name, message: e.message })); 41 } 42 }; 43 }); 44 }); 45 </script>