-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.html
40 lines (37 loc) · 1.07 KB
/
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket client</title>
<script language="JavaScript">
function log(data) {
var timestamp = '[' + new Date().toLocaleString() + '] ';
var message = timestamp + data;
console.log(message);
var result = document.getElementById('result');
var p = document.createElement("p");
p.innerText = message;
result.appendChild(p);
}
var client = new WebSocket("ws://localhost:9000");
client.onopen = function(evt) {
log('connected');
var message = "hello!";
client.send(message);
log("sent: " + message);
};
client.onclose = function(evt) {
log('disconnected');
};
client.onmessage = function(evt) {
log("received '" + evt.data + "'");
};
client.onerror = function(evt) {
log("error: " + evt.data);
}
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>