-
Notifications
You must be signed in to change notification settings - Fork 4
/
socket.html
72 lines (60 loc) · 1.91 KB
/
socket.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
<html>
<body>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<pre id="info"></pre>
<pre id="tail"></pre>
<!-- <script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
<script>
var Application = function() {
var socket = io.connect('http://127.0.0.1:8080/');
socket.on('connect', function() {
console.log('Connected to:', socket.host);
});
socket.on('message', function(message) {
console.log('Received message:', message);
if (message.filename) {
$('#info').html( '$ tail -f ' + message.filename );
};
if (message.tail) {
$('#tail').html( $('#tail').html() + message.tail );
bottom = $("#tail")[0].scrollHeight - $("#tail").height()
$('#tail').scrollTop(bottom);
}
});
return {
socket : socket
};
};
$(function() { var app = Application(); });
</script>
-->
<script>
$(function() {
// const WebSocket = require('ws')
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', function (event) {
socket.send('Hello Server!');
console.log('connection open')
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
console.log("New Message")
if (event.data) {
if (! $('pre li[data-value="' + event.data +'"]').length) {
$('#tail').append( "<li data-value='" + event.data + "'>" + event.data + "</li>");
}
// $('#tail').html( $('#tail').html() + event.data + "<br/>");
bottom = $("#tail")[0].scrollHeight - $("#tail").height()
$('#tail').scrollTop(bottom);
}
});
})
</script>
</body>
</html>