-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
119 lines (84 loc) · 1.98 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>OfflineChat-Beta</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="chat">
<input type="text" class="chat-name" placeholder="Enter Your Name">
<div class="chat-messages">
<!--<div class="chat-message">
Test:Hello World!
</div>-->
</div>
<textarea placeholder="Type Your Message"></textarea>
<div class="chat-status">Status: <span>Idle</span></div>
</div>
<script src="http://10.42.0.1:8080/socket.io/socket.io.js"></script>
<script>
(function() {
var getNode = function(s) {
return document.querySelector(s);
},
status = getNode('.chat-status span'),
messages = getNode('.chat-messages'),
textarea = getNode('.chat textarea'),
chatName = getNode('.chat-name'),
statusDefault = status.textContent,
setStatus = function(s){
status.textContent = s;
if(s !== statusDefault){
var delay = setTimeout(function() {
setStatus(statusDefault);
clearInterval(delay);
},3000);
}
};
//console.log(statusDefault);
//setStatus('Loading...');
try
{
var socket = io.connect('http://10.42.0.1:8080');
}
catch(e) {
}
if(socket !== undefined)
{
//console.log('connected');
//listen for output
socket.on('output', function(data){
//console.log(data);
if(data.length) {
// Loop through results
for (var x=0; x<data.length; x=x+1){
var message =document.createElement('div');
message.setAttribute('class','chat-message');
message.textContent= data[x].name + ': ' + data[x].message;
//Append
messages.appendChild(message);
messages.insertBefore(message, messages.firstChild);
}
}
});
// listen for status
socket.on('status', function(data){
setStatus((typeof data === 'object') ? data.message : data);
if(data.clear === true) {
textarea.value = '';
}
});
//listen for key Enter
textarea.addEventListener('keydown', function(event){
var self = this,
name = chatName.value;
if(event.which === 13 && event.shiftKey === false){
socket.emit('input', {name: name,message: self.value });
event.preventDefault();
}
});
}
})();
</script>
</body>
</html>