presentation.html (3166B)
1 <!DOCTYPE html> 2 3 <meta charset="utf-8"> 4 <link rel="author" title="Intel" href="http://www.intel.com"> 5 <link rel="author" title="He Yue" href="mailto:yue.he@intel.com"> 6 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 7 <link rel="help" href="http://w3c.github.io/presentation-api/#interface-presentationconnectionlist"> 8 <script src="../common.js"></script> 9 <script src="stash.js"></script> 10 <script> 11 const message1 = '1st'; 12 const message2 = '2nd'; 13 const message3 = new Uint8Array([51, 114, 100]); // "3rd" 14 const message4 = new Uint8Array([52, 116, 104]); // "4th" 15 const message5 = new Uint8Array([108, 97, 115, 116]); // "last" 16 17 const toUint8Array = buf => { 18 return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf; 19 } 20 21 // convert ArrayBuffer or Uint8Array into string 22 const toText = buf => { 23 const arr = toUint8Array(buf); 24 return !buf ? null : arr.reduce((result, item) => { 25 return result + String.fromCharCode(item); 26 }, ''); 27 } 28 29 // compare two ArrayBuffer or Uint8Array 30 const compare = (a, b) => { 31 const p = toUint8Array(a); 32 const q = toUint8Array(b); 33 return !!p && !!q && p.every((item, index) => { return item === q[index]; }); 34 }; 35 36 const stash = new Stash(stashIds.toReceiver, stashIds.toController); 37 38 let connection, count = 0; 39 40 const addConnection = c => { 41 let result = [], testCase; 42 connection = c; 43 count++; 44 45 connection.onmessage = event => { 46 // PresentationConnection_send-manual.https.html 47 if (testCase === 'send') { 48 if (typeof event.data === 'string') { 49 result.push({ type: 'text', data: event.data }); 50 } 51 // default value of connection.binaryType is "arraybuffer" 52 else if(event.data instanceof ArrayBuffer) { 53 result.push({ type: 'binary', data: toText(event.data) }); 54 if (compare(event.data, message5)) { 55 stash.send(JSON.stringify(result)); 56 } 57 } 58 else { 59 result.push({ type: 'error' }); 60 } 61 } 62 }; 63 64 stash.receive().then(data => { 65 testCase = data; 66 67 // PresentationConnection_send-manual.https.html 68 if (testCase === 'send') { 69 stash.send('ok'); 70 } 71 // PresentationConnection_onmessage-manual.https.html 72 else if (testCase === 'onmessage') { 73 connection.send(message1); // string 74 connection.send(message2); // string 75 connection.send(new Blob([message3])); // Blob 76 connection.send(message4.buffer); // ArrayBuffer 77 connection.send(message5); // ArrayBufferView 78 stash.receive().then(data => { 79 connection.send(message5); 80 }); 81 } 82 // PresentationConnection_onclose-manual.https.html 83 else if (testCase === 'close') { 84 connection.close(); 85 } 86 }); 87 }; 88 89 navigator.presentation.receiver.connectionList 90 .then(list => { 91 list.onconnectionavailable = evt => { 92 addConnection(evt.connection); 93 }; 94 list.connections.map(connection => { 95 addConnection(connection); 96 }); 97 }); 98 </script>