-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
158 lines (149 loc) · 5.82 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html>
<head>
<title>Chat Application</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style type="text/css">
.navbar{
padding: 10px;
border: 0px;
border-radius: 0px;
margin: 0px;
}
.container-fluid{
border: 0px;
border-radius: 0px;
}
.jumbo-container{
padding: 0px;
}
.jumbotron{
margin: 0px;
border: 0px;
border-radius: 0px;
height: 250px;
}
#name{
margin: 5px;
}
#data{
margin-bottom: 5px;
}
#datasend{
width: 90px;
margin-bottom: 5px;
}
</style>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
var socket = io.connect();
var privateRooms= [];
var currentRoom= "";
socket.on('connect', function(){
//do nothing for now
});
socket.on('updatechat', function (username, data) {
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
});
socket.on('updaterooms', function(rooms, roomSizes) {
$('#rooms').empty();
$.each(rooms, function(key, value) {
if(value == currentRoom){
$('#rooms').append('<div>' + value + "(" + roomSizes[value] + ')' + '</div>');
}
else{
$('#rooms').append('<div><a href="#" onclick="switchRoom(\''+value+'\')">' + value + "(" + roomSizes[value] + ')' + '</a></div>');
}
});
if(privateRooms.length!=0){
$('#rooms').append('<div>' + '<h4>Private Room:</h4>' + '</div>');
}
$.each(privateRooms, function(key, value) {
if(value == currentRoom){
$('#rooms').append('<div>' + value + "(" + roomSizes[value] + ')' + '</div>');
}
else if(roomSizes[value]!=0){
$('#rooms').append('<div><a href="#" onclick="switchRoom(\''+value+'\')">' + value + "(" + roomSizes[value] + ')' + '</a></div>');
}
});
});
function startChat(){
var name = $('#name').val();
socket.emit('adduser', name);
$('#name-input').hide();
$('#chat-interface').show();
}
function switchRoom(room){
socket.emit('switchRoom', room);
currentRoom = room;
}
function JoinPrivateRoom(){
var roomName = prompt("New room name?");
if(roomName==null) return;
privateRooms.push(roomName);
socket.emit('switchRoom', roomName);
currentRoom = roomName;
}
$(function(){
$('#datasend').click( function() {
var message = $('#data').val();
$('#data').val('');
socket.emit('sendchat', message);
$('#data').focus();
});
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
</script>
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Chat Application</a>
</div>
</div>
</nav>
<div id="name-input">
<div class="container-fluid text-center jumbo-container">
<div class="jumbotron">
<h3>Enter your name:</h3>
<form class="form-inline" action="" method="">
<div class="form-group">
<input type="text" id="name" class="form-control">
<input onClick="startChat()" value="Join Chat room" class="btn btn-primary" id="join-button">
</div>
</form>
</div>
</div>
</div>
<div id="chat-interface" style="display: none;">
<div class="container-fluid">
<div class="row content">
<div class="col-sm-3 sidenav">
<h4>Public Rooms:</h4>
<div id="rooms"></div>
<hr>
<div><a href="#" onClick="JoinPrivateRoom()">Join a private Room</a></div>
<hr>
</div>
<div class="col-sm-9">
<div id="conversation"><h3>Chats:</h3></div>
<form class="form-inline" action="" method="">
<div class="form-group">
<input id="data" style="width:200px;" class="form-control">
<input class="btn btn-primary" id="datasend" value="send">
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>