-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
99 lines (88 loc) · 2.77 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
93
94
95
96
97
98
99
<!DOCTYPE html>
<html>
<head>
<style>
body {}
.roomsContainer {
float:left;
width:100px;
border-right:1px solid black;
height:300px;
padding:10px;
overflow:scroll-y;
}
.conversationContainer {
float:left;
width:300px;
height:250px;
overflow:scroll-y;
padding:10px;
}
#data {
}
</style>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
//Will be adding CHATROOM LOGIC HERE
//We have Server Side funtions, now we need to add client side functions
var socket = io.connect('http://localhost:3000');
socket.on('connect', function() {
socket.emit('addUser', prompt("What's your name"));
});
socket.on('updateChat', function(username, data) {
$('#conversation').append('<b>' + username + ':</b> ' + data + '<br>');
});
socket.on('updateRooms', function(rooms, currentRoom) {
$('#rooms').empty();
$.each(rooms, function(key, value) {
if(value === currentRoom) {
$('#rooms').append('<div>'
+ value
+ '</div>');
}else {
$('#rooms').append('<div><a href="#" onclick="switchRoom(\''+ value + '\')">'
+ value
+ '</a></div>');
}
});
});
function switchRoom(room) {
socket.emit('switchRoom', room);
}
//when the page loads we need to do a few things
$(function() {
//get sent data on click
$('#datasend').click( function() {
var message = $('#data').val();
//clear the input box
$('#data').val('');
$('#data').focus();
//send it to the server
socket.emit('sendChat', message);
});
//allow the client to use enter key
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
//select the send box
$('#datasend').focus().click();
//Select the input box
$('#data').focus();
}
});
});
</script>
</head>
<body>
<div class="roomsContainer">
<b>ROOMS</b>
<div id="rooms"></div>
</div>
<div class="conversationContainer">
<div id="conversation"></div>
<input id="data" style="width:200px;" />
<input type="button" id="datasend" value="send" />
</div>
</body>
</html>