2 Copyright 2011, Google Inc.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
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
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.
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.
33 A simple console for testing WebSocket server.
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.
43 <title>WebSocket console</title>
47 var showTimeStamp = false;
49 var addressBox = null;
51 var messageBox = null;
56 function getTimeStamp() {
57 return new Date().getTime();
60 function addToLog(log) {
62 logBox.value += '[' + getTimeStamp() + '] ';
64 logBox.value += log + '\n'
65 // Large enough to keep showing the latest message.
66 logBox.scrollTop = 1000000;
69 function setbinarytype(binaryType) {
71 addToLog('Not connected');
75 socket.binaryType = binaryType;
76 addToLog('Set binaryType to ' + binaryType);
81 addToLog('Not connected');
85 socket.send(messageBox.value);
86 addToLog('> ' + messageBox.value);
87 messageBox.value = '';
92 addToLog('Not connected');
96 var files = fileBox.files;
98 if (files.length == 0) {
99 addToLog('File not selected');
103 socket.send(files[0]);
104 addToLog('> Send ' + files[0].name);
108 if ('WebSocket' in window) {
109 socket = new WebSocket(addressBox.value);
110 } else if ('MozWebSocket' in window) {
111 socket = new MozWebSocket(addressBox.value);
116 socket.onopen = function () {
119 socket.onmessage = function (event) {
120 addToLog('< ' + event.data);
122 socket.onerror = function () {
125 socket.onclose = function (event) {
126 var logMessage = 'Closed (';
127 if ((arguments.length == 1) && ('CloseEvent' in window) &&
128 (event instanceof CloseEvent)) {
129 logMessage += 'wasClean = ' + event.wasClean;
130 // code and reason are present only for
131 // draft-ietf-hybi-thewebsocketprotocol-06 and later
132 if ('code' in event) {
133 logMessage += ', code = ' + event.code;
135 if ('reason' in event) {
136 logMessage += ', reason = ' + event.reason;
139 logMessage += 'CloseEvent is not available';
141 addToLog(logMessage + ')');
144 addToLog('Connect ' + addressBox.value);
147 function closeSocket() {
149 addToLog('Not connected');
153 if (codeBox.value || reasonBox.value) {
154 socket.close(codeBox.value, reasonBox.value);
160 function printState() {
162 addToLog('Not connected');
167 'url = ' + socket.url +
168 ', readyState = ' + socket.readyState +
169 ', bufferedAmount = ' + socket.bufferedAmount);
173 var scheme = window.location.protocol == 'https:' ? 'wss://' : 'ws://';
174 var defaultAddress = scheme + window.location.host + '/echo';
176 addressBox = document.getElementById('address');
177 logBox = document.getElementById('log');
178 messageBox = document.getElementById('message');
179 fileBox = document.getElementById('file');
180 codeBox = document.getElementById('code');
181 reasonBox = document.getElementById('reason');
183 addressBox.value = defaultAddress;
185 if ('MozWebSocket' in window) {
186 addToLog('Use MozWebSocket');
187 } else if (!('WebSocket' in window)) {
188 addToLog('WebSocket is not available');
193 <body onload="init()">
195 <form action="#" onsubmit="connect(); return false;">
196 <input type="text" id="address" size="40">
197 <input type="submit" value="connect">
198 <input type="button" value="print state" onclick="printState();">
201 <textarea id="log" rows="10" cols="40" readonly></textarea>
203 <form action="#" onsubmit="send(); return false;">
204 <input type="text" id="message" size="40">
205 <input type="submit" value="send">
208 <form action="#" onsubmit="sendfile(); return false;">
209 <input type="file" id="file" size="40">
210 <input type="submit" value="send file">
217 onclick="setbinarytype('blob')" checked>blob
221 onclick="setbinarytype('arraybuffer')">arraybuffer
225 <input type="checkbox"
227 value="showtimestamp"
228 onclick="showTimeStamp = this.checked">Show time stamp
231 <form action="#" onsubmit="closeSocket(); return false;">
232 Code <input type="text" id="code" size="10">
233 Reason <input type="text" id="reason" size="20">
234 <input type="submit" value="close">