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