-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo-client.html
183 lines (169 loc) · 5.36 KB
/
echo-client.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
table {
border: 0;
}
.commslog-data {
font-family: Consolas, Courier New, Courier, monospace;
}
.commslog-server {
background-color: red;
color: white;
}
.commslog-client {
background-color: green;
color: white;
}
</style>
</head>
<body>
<h1>WebSocket Sample Application</h1>
<p id="stateLabel">Ready to connect...</p>
<div>
<label for="connectionUrl">WebSocket Server URL:</label>
<input id="connectionUrl" />
<button id="connectButton" type="submit">Connect</button>
</div>
<p></p>
<div>
<label for="sendMessage">Message to send:</label>
<input id="sendMessage" disabled />
<button id="sendButton" type="submit" disabled>Send</button>
<button id="closeButton" disabled>Close Socket</button>
</div>
<h2>Communication Log</h2>
<table style="width: 800px">
<thead>
<tr>
<td style="width: 100px">From</td>
<td style="width: 100px">To</td>
<td>Data</td>
</tr>
</thead>
<tbody id="commsLog"></tbody>
</table>
<script>
var connectionUrl = document.getElementById('connectionUrl');
var connectButton = document.getElementById('connectButton');
var stateLabel = document.getElementById('stateLabel');
var sendMessage = document.getElementById('sendMessage');
var sendButton = document.getElementById('sendButton');
var commsLog = document.getElementById('commsLog');
var closeButton = document.getElementById('closeButton');
var socket;
var scheme = document.location.protocol === 'https:' ? 'wss' : 'ws';
var port = document.location.port ? ':' + document.location.port : '';
connectionUrl.value =
scheme + '://' + document.location.hostname + port + '/ws';
function updateState() {
function disable() {
sendMessage.disabled = true;
sendButton.disabled = true;
closeButton.disabled = true;
}
function enable() {
sendMessage.disabled = false;
sendButton.disabled = false;
closeButton.disabled = false;
}
connectionUrl.disabled = true;
connectButton.disabled = true;
if (!socket) {
disable();
} else {
switch (socket.readyState) {
case WebSocket.CLOSED:
stateLabel.innerHTML = 'Closed';
disable();
connectionUrl.disabled = false;
connectButton.disabled = false;
break;
case WebSocket.CLOSING:
stateLabel.innerHTML = 'Closing...';
disable();
break;
case WebSocket.CONNECTING:
stateLabel.innerHTML = 'Connecting...';
disable();
break;
case WebSocket.OPEN:
stateLabel.innerHTML = 'Open';
enable();
break;
default:
stateLabel.innerHTML =
'Unknown WebSocket State: ' + htmlEscape(socket.readyState);
disable();
break;
}
}
}
closeButton.onclick = function () {
if (!socket || socket.readyState !== WebSocket.OPEN) {
alert('socket not connected');
}
socket.close(1000, 'Closing from client');
};
sendButton.onclick = function () {
if (!socket || socket.readyState !== WebSocket.OPEN) {
alert('socket not connected');
}
var data = sendMessage.value;
socket.send(data);
commsLog.innerHTML +=
'<tr>' +
'<td class="commslog-client">Client</td>' +
'<td class="commslog-server">Server</td>' +
'<td class="commslog-data">' +
htmlEscape(data) +
'</td></tr>';
};
connectButton.onclick = function () {
stateLabel.innerHTML = 'Connecting';
socket = new WebSocket(connectionUrl.value, 'esgrima');
// socket = new WebSocket(connectionUrl.value);
socket.onopen = function (event) {
updateState();
commsLog.innerHTML +=
'<tr>' +
'<td colspan="3" class="commslog-data">Connection opened</td>' +
'</tr>';
};
socket.onclose = function (event) {
updateState();
commsLog.innerHTML +=
'<tr>' +
'<td colspan="3" class="commslog-data">Connection closed. Code: ' +
htmlEscape(event.code) +
'. Reason: ' +
htmlEscape(event.reason) +
'</td>' +
'</tr>';
};
socket.onerror = updateState;
socket.onmessage = function (event) {
commsLog.innerHTML +=
'<tr>' +
'<td class="commslog-server">Server</td>' +
'<td class="commslog-client">Client</td>' +
'<td class="commslog-data">' +
htmlEscape(event.data) +
'</td></tr>';
};
};
function htmlEscape(str) {
return str
.toString()
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
</script>
</body>
</html>