1<!-- 2 #%L 3 NanoHttpd-Websocket 4 %% 5 Copyright (C) 2012 - 2015 nanohttpd 6 %% 7 Redistribution and use in source and binary forms, with or without modification, 8 are permitted provided that the following conditions are met: 9 10 1. Redistributions of source code must retain the above copyright notice, this 11 list of conditions and the following disclaimer. 12 13 2. Redistributions in binary form must reproduce the above copyright notice, 14 this list of conditions and the following disclaimer in the documentation 15 and/or other materials provided with the distribution. 16 17 3. Neither the name of the nanohttpd nor the names of its contributors 18 may be used to endorse or promote products derived from this software without 19 specific prior written permission. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 29 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 30 OF THE POSSIBILITY OF SUCH DAMAGE. 31 #L% 32 --> 33<html> 34<head> 35 <meta charset="utf-8"/> 36 <title>WebSocket Test</title> 37 <script language="javascript" type="text/javascript"> 38 var wsUri = "ws://localhost:9090/"; 39 var output; 40 function init() { 41 output = document.getElementById("output"); 42 testWebSocket(); 43 } 44 function testWebSocket() { 45 websocket = new WebSocket(wsUri); 46 websocket.onopen = function (evt) { 47 onOpen(evt) 48 }; 49 websocket.onclose = function (evt) { 50 onClose(evt) 51 }; 52 websocket.onmessage = function (evt) { 53 onMessage(evt) 54 }; 55 websocket.onerror = function (evt) { 56 onError(evt) 57 }; 58 } 59 function onOpen(evt) { 60 writeToScreen("CONNECTED"); 61 doSend("WebSocket rocks"); 62 } 63 function onClose(evt) { 64 writeToScreen("DISCONNECTED"); 65 } 66 function onMessage(evt) { 67 writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>'); 68 websocket.close(); 69 } 70 function onError(evt) { 71 writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); 72 } 73 function doSend(message) { 74 writeToScreen("SENT: " + message); 75 websocket.send(message); 76 } 77 function writeToScreen(message) { 78 var pre = document.createElement("p"); 79 pre.style.wordWrap = "break-word"; 80 pre.innerHTML = message; 81 output.appendChild(pre); 82 } 83 window.addEventListener("load", init, false); </script> 84</head> 85<body> 86<h2>WebSocket Test</h2> 87 88<div id="output"></div> 89</body> 90</html>