-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.html
56 lines (44 loc) · 1.51 KB
/
client.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>
<title>Redis Chat</title>
<link href="/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<style>
input[type="text"] {
width: 95%;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div id="chat-window">
<ul id="chat"></ul>
</div>
<form method="get" action="" onsubmit="return false;">
<input type="text" placeholder="Type here to chat!">
<input type="submit">
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function(){
var socket = io.connect('http://localhost');
socket.on("initial load", function(data){
data.forEach(function(message){
var chat = $('#chat'),
li = $('<li>'+message+'</li>');
chat.append(li);
});
});
socket.on("broadcast", function(data){
$('#chat').append($('<li>'+data.message+'</li>'));
});
$('form').submit(function(e){
var input = $('input[type=text]', this),
message = input.val();
socket.emit("new message", {message: message});
input.val("");
});
});
</script>
</body>
</html>