PresentationConnection_onmessage-manual.https.html (2530B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Receiving a message through PresentationConnection</title> 4 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 5 <link rel="help" href="http://w3c.github.io/presentation-api/#receiving-a-message-through-presentationconnection"> 6 <script src="common.js"></script> 7 <script src="support/stash.js"></script> 8 9 <p>Click the button below and select the available presentation display, to start the manual test.</p> 10 <button id="presentBtn">Start Presentation Test</button> 11 12 <script> 13 const message1 = '1st'; 14 const message2 = '2nd'; 15 const message3 = new Uint8Array([51, 114, 100]); // "3rd" 16 const message4 = new Uint8Array([52, 116, 104]); // "4th" 17 const message5 = new Uint8Array([108, 97, 115, 116]); // "last" 18 19 let connection; 20 const presentBtn = document.getElementById('presentBtn'); 21 presentBtn.onclick = () => { 22 presentBtn.disabled = true; 23 const stash = new Stash(stashIds.toController, stashIds.toReceiver); 24 const request = new PresentationRequest('support/PresentationConnection_onmessage_receiving-ua.html'); 25 26 request.start().then(c => { 27 c.onconnect = () => { 28 connection = c; 29 connection.send(message1); // string 30 connection.send(message2); // string 31 connection.send(new Blob([message3])); // Blob 32 connection.send(message4.buffer); // ArrayBuffer 33 connection.send(message5); // ArrayBufferView 34 return stash.receive().then(data => { 35 const result = JSON.parse(data); 36 if (result.type === 'blob') { 37 connection.send(message5); 38 return stash.receive(); 39 } 40 else 41 return data; 42 }).then(result => { 43 const json = JSON.parse(result); 44 45 // notify receiver's results of a parent window (e.g. test runner) 46 if (window.opener && 'completion_callback' in window.opener) { 47 window.opener.completion_callback(json.tests, json.status); 48 } 49 // display receiver's results as HTML 50 const log = document.createElement('div'); 51 log.id = 'log'; 52 log.innerHTML = json.log; 53 document.body.appendChild(log); 54 55 connection.onconnect = () => { connection.terminate(); }; 56 if (connection.state === 'closed') 57 request.reconnect(connection.id); 58 else 59 connection.terminate(); 60 }); 61 }; 62 }); 63 }; 64 </script>