-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
92 lines (82 loc) · 2.69 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
<!doctype html>
<html>
<head>
<title>Chat with socket.io and node.js</title>
<style>
#chat{
height:500px;
}
#contentWrap{
display: none;
}
#chatWrap{
float:left;
border: 1px #000 solid;
}
</style>
</head>
<body>
<div id ="nickWrap">
<p>Enter the username</p>
<p id="nickError"></p>
<form id ="setNick">
<input size="35" id="nickname"></input>
<input type="submit"></input>
</form>
</div>
<div id="contentWrap">
<div id="chatWrap">
<div id="chat"></div>
<form id="send-message">
<input size="35" id="message"></input>
<input type="submit"></input>
</form>
</div>
<div id="users"></div>
</div>
<script src="http://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket=io();
var $nickForm=$('#setNick');
var $nickError=$('#nickError');
var $nickBox=$('#nickname');
var $users = $('#users');
var $messageForm=$('#send-message');
var $messageBox=$('#message');
var $chat= $('#chat');
$nickForm.submit(function(e){
e.preventDefault();
socket.emit('new user', $nickBox.val(), function(data){
if(data){
$('#nickWrap').hide();
$('#contentWrap').show();
} else{
$nickError.html("that username is already taken! try again.");
}
});
$nickBox.val('');
});
socket.on('usernames',function(data){
var html='';
for(i=0; i < data.length; i++){
html +=data[i]+ '<br/>'
}
$users.html(html);
});
$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message',$messageBox.val());
$messageBox.val('');
});
/*socket.on('load old messages',function(docs){
for(var i=0; i<docs.length; i++){
$chat.append(data.nick +":" + data.msg + "<br/>");
}
});*/
socket.on('new message', function(data){
$chat.append(data.nick +":" + data.msg + "<br/>");
});
</script>
</body>
</html>