-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
57 lines (56 loc) · 2.38 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
<!doctype html>
<html>
<meta charset="UTF-8"></meta>
<head>
<title> Socket.IO chat app </title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: darkseagreen; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: #DCDCDC; 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 }
</style>
</head>
<body>
<div id="username-taken"></div>
<div id="newUser">
<p style="align-text: center;"> Tervetuloa Tänne :) </p>
<input id="name" type="text" name="name" value="" placeholder="Saisinko Nickisi">
<button type="button" name="namebutton" onclick="setUsername()"> Nick </button>
</div>
<div id="msgs" style="visibility: hidden;">
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Lähetä</button>
</form>
</div>
<script src ="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
var user;
function setUsername(){
socket.emit('setUsername', document.getElementById('name').value);
};
socket.on('userTaken', function(data){
document.getElementById('username-taken').innerHTML = data;
});
socket.on('setUser', function(data){
user = document.getElementById('name').value;
document.getElementById('newUser').style.visibility = 'hidden';
document.getElementById('msgs').style.visibility = 'visible';
});
$('form').submit(function(){
socket.emit('chat message', user, $('#m').val());
$('#m').val('');
return false;
});
socket.on('broadcast', function(data){
$('#messages').append($('<li>').text(data.description));
});
</script>
</body>
</html>