-
Notifications
You must be signed in to change notification settings - Fork 329
/
ws.html
157 lines (138 loc) · 4.54 KB
/
ws.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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>WebSocket client</title>
<script src="/admin/jquery.min.js"></script>
<script type="text/javascript">
var websocket;
$(document).ready(init);
function init() {
$('#server').val("3238353554348D05");
if(!("WebSocket" in window)){
$('#status').append('<p><span style="color: red;">websockets are not supported </span></p>');
$("#navigation").hide();
} else {
$('#status').hide();
connect();
};
$("#connected").hide();
$("#content").hide();
};
function ascii_to_hexa(data)
{
var arr1 = [];
var buffer = new Uint8Array(data);
for (var n=0; n < buffer.length; n++)
{
var hex = buffer[n].toString(16);
if (hex.length < 2) hex = "0" + hex;
arr1.push(hex);
}
return arr1.join('');
}
function hexa_to_binary(hex)
{
var bytes = new Uint8Array(hex.length/2);
for (var n=0; n < hex.length/2; n++)
{
bytes[n] = parseInt(hex.substr(2*n, 2), 16);
}
return bytes;
}
function connect()
{
if(window.location.protocol == "http:")
wsProto = "ws://";
else if(window.location.protocol == "https:")
wsProto = "wss://";
wsHost = wsProto + window.location.host + $("#uri").val();
websocket = new WebSocket(wsHost);
showScreen('Connecting to: ' + wsHost);
websocket.binaryType = 'arraybuffer';
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
};
function disconnect() {
websocket.close();
};
function toggle_connection(){
if(websocket.readyState == websocket.OPEN){
disconnect();
} else {
connect();
};
};
function sendTxt() {
if(websocket.readyState == websocket.OPEN) {
txt = $("#send_txt").val();
if($("#format").val() == "raw") {
websocket.send(hexa_to_binary(txt).buffer);
} else {
websocket.send(txt);
}
showScreen('<span style="color: green;">SEND: ' + txt + '</span>');
} else {
showScreen('websocket is not connected');
};
};
function onOpen(evt) {
showScreen('<span style="color: green;">CONNECTED</span>');
$("#connected").fadeIn('slow');
$("#content").fadeIn('slow');
};
function onClose(evt) {
showScreen('<span style="color: red;">DISCONNECTED</span>');
};
function onMessage(evt) {
if($("#format").val() == "raw") {
showScreen('<span style="color: blue;">RECEIVED: ' + ascii_to_hexa(evt.data) +'</span>');
} else if($("#format").val() == "json") {
var msg = JSON.parse(evt.data);
showScreen('<span style="color: blue;">RECEIVED: ' + JSON.stringify(msg) +'</span>');
} else {
showScreen('<span style="color: blue;">RECEIVED: ' + evt.data +'</span>');
}
};
function onError(evt) {
showScreen('<span style="color: red;">ERROR: ' + evt.data+ '</span>');
};
function showScreen(txt) {
var m = new Date();
var dateString = m.toLocaleDateString()+" "+m.toLocaleTimeString();
$('#output').prepend(dateString + ' ' + txt + '<br>');
};
function clearScreen()
{
$('#output').html("");
};
</script>
</head>
<body>
<div id="header">
<h1>Websocket client</h1>
<div id="status"></div>
</div>
<div id="navigation">
<p id="connecting">
<input type='text' id="uri" placeholder="/uri/path"></input>
<select id="format">
<option value="raw" selected="true">Raw</option>
<option value="json">JSON</option>
<option value="text">Text</option>
</select>
<button type="button" onclick="toggle_connection()">connection</button>
</p>
<p id="connected">
Hex String or JSON<br>
<input type='text' id="send_txt" size=50></input>
<button type="button" onclick="sendTxt();">send</button>
</p>
<div id="content">
<button id="clear" onclick="clearScreen()" >Clear text</button>
<p id="output"></p>
</div>
</div>
</body>
</html>