websocket-worker.js (696B)
1 let port; 2 let received = false; 3 4 function reportFailure(details) { 5 port.postMessage('FAIL: ' + details); 6 } 7 8 onmessage = event => { 9 port = event.source; 10 11 const ws = new WebSocket('wss://{{host}}:{{ports[wss][0]}}/echo'); 12 ws.onopen = () => { 13 ws.send('Hello'); 14 }; 15 ws.onmessage = msg => { 16 if (msg.data !== 'Hello') { 17 reportFailure('Unexpected reply: ' + msg.data); 18 return; 19 } 20 21 received = true; 22 ws.close(); 23 }; 24 ws.onclose = (event) => { 25 if (!received) { 26 reportFailure('Closed before receiving reply: ' + event.code); 27 return; 28 } 29 30 port.postMessage('PASS'); 31 }; 32 ws.onerror = () => { 33 reportFailure('Got an error event'); 34 }; 35 };