forked from jmg/node-simple-chat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.html
54 lines (49 loc) · 1.74 KB
/
chat.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
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
var socket = io.connect('http://localhost:3000');
socket.on('msg', function(data) {
var msg = JSON.parse(data);
appendMsg(msg);
});
socket.on('init', function(data) {
var messages = JSON.parse(data)
for (i in messages)
appendMsg(messages[i])
});
function appendMsg(msg) {
$('#msgs').append(function() {
var div = $('<div>');
div.html('<b>' + msg.username + ':</b> ' + msg.message);
return div;
});
$('#msgs')[0].scrollTop = $('#msgs')[0].scrollHeight;
}
function sendMsg() {
var msg = {};
$.each($('#chat').serializeArray(), function(i,v) {
msg[v.name] = v.value;
});
$("#msg").val("");
appendMsg(msg);
socket.emit('msg', JSON.stringify(msg));
}
</script>
</head>
<body>
<div>
<p class="chat-title" onclick="javascript: toggleChat();">Real Time Chat</p>
<div id="msgs"></div>
<div id="form">
<form id="chat" onsubmit="sendMsg(); return false;">
<label for="username">Username: </label><input name="username" type="text"><br/>
<label for="msg"> Message: </label><input id="msg" type="text" name="message"/><br/>
<input type="submit"/>
</form>
</div>
</div>
</body>
</html>