-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.html
More file actions
68 lines (55 loc) · 1.77 KB
/
client.html
File metadata and controls
68 lines (55 loc) · 1.77 KB
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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest() {
var log = document.getElementById('log');
var content = document.getElementById('content');
if ("WebSocket" in window) {
log.innerHTML = log.innerHTML + "WebSocket didukung oleh browser <br>";
// Buat koneksi ke server, subscribe pada topic 'demo'
var ws = new WebSocket("ws://127.0.0.1:4444");
// Apabila ada data masuk, tampilkan pada div
ws.onmessage = function(e) {
log.innerHTML = log.innerHTML + "Data masuk <br>";
content.innerHTML = content.innerHTML + e.data + "<br>";
};
ws.onopen = function() {
// Coba kirim data
ws.send('{"action":"sub","topic":"demo","source":"browser"}');
log.innerHTML = log.innerHTML + "Data terkirim<br>";
};
ws.onclose = function() {
// WebSocket terputus
log.innerHTML = log.innerHTML + "Koneksi terputus...<br>";
};
} else {
log.innerHTML = log.innerHTML + "WebSocket tidak didukung oleh browser<br>";
}
}
</script>
<style>
* {
font-family: arial, sans-serif;
font-size: 18px;
}
#content {
padding: 20px;
background-color: #eee;
}
#log {
margin-top:20px;
padding: 20px;
background-color: #fafafa;
}
body {
margin: 30px;
}
</style>
</head>
<body onload="WebSocketTest()">
<h5>Data dari publish server akan muncul disini:</h5>
<div id="content"></div>
<div id="log"></div>
</body>
</html>