-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
50 lines (46 loc) · 1.85 KB
/
index.html
File metadata and controls
50 lines (46 loc) · 1.85 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
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
.nickname-container { background-color: #000000; color: rgb(130, 224, 255); padding: 5px }
</style>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var sendMessage = function () {
var message = document.getElementById('message').value;
var nickname = document.getElementById('nickname').value;
if (message && nickname) {
var payload = { 'message': message, 'nickname': nickname };
console.debug('About to send ', payload);
socket.emit('chat message', payload);
document.getElementById('message').value = '';
}
}
socket.on('chat message', function(payload){
var node = document.createElement("li");
var text = document.createTextNode(payload.nickname + ': ' + payload.message);
node.appendChild(text);
document.getElementById('messages').appendChild(node);
});
</script>
</head>
<body>
<div class="nickname-container">
Nickname <input type="text" id="nickname">
</div>
<ul id="messages"></ul>
<form action="javascript:sendMessage()">
<input id="message" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>