-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.html
202 lines (174 loc) · 5.77 KB
/
console.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!--
Copyright 2011, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!--
A simple console for testing WebSocket server.
Type an address into the top text input and click connect to establish
WebSocket. Then, type some message into the bottom text input and click send
to send the message. Received/sent messages and connection state will be shown
on the middle textarea.
-->
<html>
<head>
<meta charset="ISO-8859-1"/>
<!--meta charset="UTF-8"/-->
<title>WebSocket console</title>
<script>
var socket = null;
var addressBox = null;
var logBox = null;
var messageBox = null;
var codeBox = null;
var reasonBox = null;
function addToLog(log) {
logBox.value += log + '\n'
// Large enough to keep showing the latest message.
logBox.scrollTop = 1000000;
}
function send() {
if (!socket) {
addToLog('Not connected');
return;
}
socket.send(messageBox.value);
addToLog('> ' + messageBox.value);
//messageBox.value = '';
/*data = messageBox.value.map(function (num) {
return String.fromCharCode(num);
}).join('');
addToLog('> ' + data);*/
//var data = messageBox.value.charCodeAt(38);
//addToLog('> ' + data);
}
function connect() {
if ('WebSocket' in window) {
socket = new WebSocket(addressBox.value);
} else if ('MozWebSocket' in window) {
socket = new MozWebSocket(addressBox.value);
} else {
return;
}
socket.binaryType = "arraybuffer";
socket.onopen = function () {
addToLog('Opened');
};
socket.onmessage = function (event) {
addToLog('< ' + event.data);
};
socket.onerror = function () {
addToLog('Error');
};
socket.onclose = function (event) {
var logMessage = 'Closed (';
if ((arguments.length == 1) && ('CloseEvent' in window) &&
(event instanceof CloseEvent)) {
logMessage += 'wasClean = ' + event.wasClean;
// code and reason are present only for
// draft-ietf-hybi-thewebsocketprotocol-06 and later
if ('code' in event) {
logMessage += ', code = ' + event.code;
}
if ('reason' in event) {
logMessage += ', reason = ' + event.reason;
}
} else {
logMessage += 'CloseEvent is not available';
}
addToLog(logMessage + ')');
};
addToLog('Connect ' + addressBox.value);
}
function closeSocket() {
if (!socket) {
addToLog('Not connected');
return;
}
if (codeBox.value || reasonBox.value) {
socket.close(codeBox.value, reasonBox.value);
} else {
socket.close();
}
}
function printState() {
if (!socket) {
addToLog('Not connected');
return;
}
addToLog(
'url = ' + socket.url +
', readyState = ' + socket.readyState +
', bufferedAmount = ' + socket.bufferedAmount);
}
function init() {
var scheme = window.location.protocol == 'https:' ? 'wss://' : 'ws://';
var defaultAddress = scheme + window.location.host + '/echo';
addressBox = document.getElementById('address');
logBox = document.getElementById('log');
messageBox = document.getElementById('message');
codeBox = document.getElementById('code');
reasonBox = document.getElementById('reason');
addressBox.value = defaultAddress;
if ('MozWebSocket' in window) {
addToLog('Use MozWebSocket');
} else if (!('WebSocket' in window)) {
addToLog('WebSocket is not available');
}
}
function sendPhoto() {
size = 500
var ba = new Uint8Array(size);
for (var i = 0; i < size; i++) {
ba[i] = i;
}
m = ba.buffer;
socket.send(m);
}
</script>
</head>
<body onload="init()">
<form action="#" onsubmit="connect(); return false;">
<input type="text" id="address" size="40">
<input type="submit" value="connect">
<input type="button" value="print state" onclick="printState();">
</form>
<textarea id="log" rows="10" cols="40" readonly></textarea>
<form action="#" onsubmit="send(); return false;">
<input type="text" id="message" size="40">
<input type="submit" value="send">
</form>
<form action="#" onsubmit="closeSocket(); return false;">
Code <input type="text" id="code" size="10">
Reason <input type="text" id="reason" size="20">
<input type="submit" value="close">
</form>
<br>
<form action="#" onsubmit="sendPhoto(); return false;">
Binary <input type="text" id="code" size="10">
<input type="submit" value="binary">
</form>
</body>
</html>