| tyoshino@chromium.org | 7805671 | 2010-12-16 07:41:26 | [diff] [blame^] | 1 | <!-- |
| 2 | Copyright 2010, Google Inc. |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are |
| 7 | met: |
| 8 | |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following disclaimer |
| 13 | in the documentation and/or other materials provided with the |
| 14 | distribution. |
| 15 | * Neither the name of Google Inc. nor the names of its |
| 16 | contributors may be used to endorse or promote products derived from |
| 17 | this software without specific prior written permission. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | --> |
| 31 | |
| 32 | <!-- |
| 33 | A simple console for testing WebSocket server. |
| 34 | |
| 35 | Type an address into the top text input and click connect to establish |
| 36 | WebSocket. Then, type some message into the bottom text input and click send |
| 37 | to send the message. Received/sent messages and connection state will be shown |
| 38 | on the middle textarea. |
| 39 | --> |
| 40 | |
| 41 | <html> |
| 42 | <head> |
| 43 | <title>WebSocket console</title> |
| 44 | <script> |
| 45 | var socket = null; |
| 46 | |
| 47 | var addressBox = null; |
| 48 | var logBox = null; |
| 49 | var messageBox = null; |
| 50 | |
| 51 | function addToLog(log) { |
| 52 | logBox.value += log + '\n' |
| 53 | // Large enough to keep showing the latest message. |
| 54 | logBox.scrollTop = 1000000; |
| 55 | } |
| 56 | |
| 57 | function send() { |
| 58 | socket.send(messageBox.value); |
| 59 | addToLog('> ' + messageBox.value); |
| 60 | messageBox.value = ''; |
| 61 | } |
| 62 | |
| 63 | function connect() { |
| 64 | socket = new WebSocket(addressBox.value); |
| 65 | |
| 66 | socket.onopen = function () { |
| 67 | addToLog('Opened'); |
| 68 | }; |
| 69 | socket.onmessage = function (event) { |
| 70 | addToLog('< ' + event.data); |
| 71 | }; |
| 72 | socket.onerror = function () { |
| 73 | addToLog('Error'); |
| 74 | }; |
| 75 | socket.onclose = function () { |
| 76 | addToLog('Closed'); |
| 77 | }; |
| 78 | |
| 79 | addToLog('Connect ' + addressBox.value); |
| 80 | } |
| 81 | |
| 82 | function init() { |
| 83 | var scheme = window.location.protocol == 'https:' ? 'wss://' : 'ws://'; |
| 84 | var defaultAddress = scheme + window.location.host + '/echo'; |
| 85 | |
| 86 | addressBox = document.getElementById('address'); |
| 87 | logBox = document.getElementById('log'); |
| 88 | messageBox = document.getElementById('message'); |
| 89 | |
| 90 | addressBox.value = defaultAddress; |
| 91 | } |
| 92 | </script> |
| 93 | </head> |
| 94 | <body onload="init()"> |
| 95 | |
| 96 | <form action="#" onsubmit="connect(); return false;"> |
| 97 | <input type="text" id="address" size="40"> |
| 98 | <input type="submit" value="connect"> |
| 99 | </form> |
| 100 | |
| 101 | <textarea id="log" rows="10" cols="40" readonly></textarea> |
| 102 | |
| 103 | <form action="#" onsubmit="send(); return false;"> |
| 104 | <input type="text" id="message" size="40"> |
| 105 | <input type="submit" value="send"> |
| 106 | </form> |
| 107 | |
| 108 | </body> |
| 109 | </html> |