blob: 632554b4d01295187e79b9f05a4fa56fdb12d7e2 [file] [log] [blame]
tyoshino@chromium.org78056712010-12-16 07:41:261<!--
2Copyright 2010, Google Inc.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 * Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12copyright notice, this list of conditions and the following disclaimer
13in the documentation and/or other materials provided with the
14distribution.
15 * Neither the name of Google Inc. nor the names of its
16contributors may be used to endorse or promote products derived from
17this software without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30-->
31
32<!--
33A simple console for testing WebSocket server.
34
35Type an address into the top text input and click connect to establish
36WebSocket. Then, type some message into the bottom text input and click send
37to send the message. Received/sent messages and connection state will be shown
38on the middle textarea.
39-->
40
41<html>
42<head>
43<title>WebSocket console</title>
44<script>
45var socket = null;
46
47var addressBox = null;
48var logBox = null;
49var messageBox = null;
50
51function addToLog(log) {
52 logBox.value += log + '\n'
53 // Large enough to keep showing the latest message.
54 logBox.scrollTop = 1000000;
55}
56
57function send() {
58 socket.send(messageBox.value);
59 addToLog('> ' + messageBox.value);
60 messageBox.value = '';
61}
62
63function 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.org6089e1e2011-05-09 09:15:3175 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.org78056712010-12-16 07:41:2692 };
93
94 addToLog('Connect ' + addressBox.value);
95}
96
tyoshino@chromium.org28e12002010-12-24 09:40:4897function closeSocket() {
98 socket.close();
99}
100
tyoshino@chromium.org78056712010-12-16 07:41:26101function 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.org6089e1e2011-05-09 09:15:31110
111 if (!('WebSocket' in window)) {
112 addToLog('WebSocket is not available');
113 }
tyoshino@chromium.org78056712010-12-16 07:41:26114}
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.org28e12002010-12-24 09:40:48122<input type="button" value="close" onclick="closeSocket();">
tyoshino@chromium.org78056712010-12-16 07:41:26123</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>