| 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 | }; |
| tyoshino@chromium.org | 6089e1e | 2011-05-09 09:15:31 | [diff] [blame^] | 75 | socket.onclose = function (event) { |
| 76 | var logMessage = 'Closed ('; |
| 77 | if ((arguments.length == 1) && ('CloseEvent' in window) && |
| 78 | (event instanceof CloseEvent)) { |
| 79 | logMessage += 'wasClean = ' + event.wasClean; |
| 80 | // code and reason are present only for |
| 81 | // draft-ietf-hybi-thewebsocketprotocol-06 and later |
| 82 | if ('code' in event) { |
| 83 | logMessage += ', code = ' + event.code; |
| 84 | } |
| 85 | if ('reason' in event) { |
| 86 | logMessage += ', reason = ' + event.reason; |
| 87 | } |
| 88 | } else { |
| 89 | logMessage += 'CloseEvent is not available'; |
| 90 | } |
| 91 | addToLog(logMessage + ')'); |
| tyoshino@chromium.org | 7805671 | 2010-12-16 07:41:26 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | addToLog('Connect ' + addressBox.value); |
| 95 | } |
| 96 | |
| tyoshino@chromium.org | 28e1200 | 2010-12-24 09:40:48 | [diff] [blame] | 97 | function closeSocket() { |
| 98 | socket.close(); |
| 99 | } |
| 100 | |
| tyoshino@chromium.org | 7805671 | 2010-12-16 07:41:26 | [diff] [blame] | 101 | function init() { |
| 102 | var scheme = window.location.protocol == 'https:' ? 'wss://' : 'ws://'; |
| 103 | var defaultAddress = scheme + window.location.host + '/echo'; |
| 104 | |
| 105 | addressBox = document.getElementById('address'); |
| 106 | logBox = document.getElementById('log'); |
| 107 | messageBox = document.getElementById('message'); |
| 108 | |
| 109 | addressBox.value = defaultAddress; |
| tyoshino@chromium.org | 6089e1e | 2011-05-09 09:15:31 | [diff] [blame^] | 110 | |
| 111 | if (!('WebSocket' in window)) { |
| 112 | addToLog('WebSocket is not available'); |
| 113 | } |
| tyoshino@chromium.org | 7805671 | 2010-12-16 07:41:26 | [diff] [blame] | 114 | } |
| 115 | </script> |
| 116 | </head> |
| 117 | <body onload="init()"> |
| 118 | |
| 119 | <form action="#" onsubmit="connect(); return false;"> |
| 120 | <input type="text" id="address" size="40"> |
| 121 | <input type="submit" value="connect"> |
| tyoshino@chromium.org | 28e1200 | 2010-12-24 09:40:48 | [diff] [blame] | 122 | <input type="button" value="close" onclick="closeSocket();"> |
| tyoshino@chromium.org | 7805671 | 2010-12-16 07:41:26 | [diff] [blame] | 123 | </form> |
| 124 | |
| 125 | <textarea id="log" rows="10" cols="40" readonly></textarea> |
| 126 | |
| 127 | <form action="#" onsubmit="send(); return false;"> |
| 128 | <input type="text" id="message" size="40"> |
| 129 | <input type="submit" value="send"> |
| 130 | </form> |
| 131 | |
| 132 | </body> |
| 133 | </html> |