-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
60 lines (48 loc) · 1.36 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
<html ng-app="chatApp">
<head>
<title>DKO CHAT</title>
</head>
<body>
<h1>DKO CHAT</h1>
<p ng-controller='chatCtrl'></p>
<ul id="messages"></ul>
<form action="">
<label>Name</label>
<input id="name" autocomplete="off" />
<label>Message</label>
<input id="data" autocomplete="off" /><button>Send</button>
</form>
<!-- JavaScript -->
<script src='/socket.io/socket.io.js'></script>
<script src='node_modules/angular/angular.js'></script>
<script src='angular.js'></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
// append message function
function msgAppend(content){
$('#messages').append($('<li>').text(content));
}
// socket.io setup
var socket = io();
// emit a join message when user joins chat
socket.emit('join', name);
// emit message data when user clicks submit button
$('form').submit(function(){
socket.emit('chat message', {
name: $('#name').val(),
data: $('#data').val()
});
$('#data').val('');
return false;
});
// print user name of user who joined chat on join
socket.on('join', function(name){
msgAppend( name+" has joined the chat." );
});
// print chat message
socket.on('chat message', function(msg){
msgAppend( msg.name+": "+msg.data );
});
</script>
</body>
</html>