websocket_basic_worker.js (1037B)
1 onmessage = function (event) { 2 if (event.data != 0) { 3 var worker = new Worker("websocket_basic_worker.js"); 4 worker.onmessage = function (e) { 5 postMessage(e.data); 6 }; 7 8 worker.postMessage(event.data - 1); 9 return; 10 } 11 12 let status = false; 13 try { 14 if (WebSocket instanceof Object) { 15 status = true; 16 } 17 } catch (e) {} 18 19 postMessage({ 20 type: "status", 21 status, 22 msg: "WebSocket object:" + WebSocket, 23 }); 24 25 var ws = new WebSocket( 26 "ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_hello" 27 ); 28 ws.onopen = function () { 29 postMessage({ type: "status", status: true, msg: "OnOpen called" }); 30 ws.send("data"); 31 }; 32 33 ws.onclose = function () {}; 34 35 ws.onerror = function () { 36 postMessage({ type: "status", status: false, msg: "onerror called!" }); 37 }; 38 39 ws.onmessage = function (e) { 40 postMessage({ 41 type: "status", 42 status: e.data == "Hello world!", 43 msg: "Wrong data", 44 }); 45 ws.close(); 46 postMessage({ type: "finish" }); 47 }; 48 };