-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
175 lines (165 loc) · 6.3 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 80%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#logout{ width: 100%; height: 4.5%; background: #FFBFA1; border: none; padding: 10px; position: absolute; bottom:0; right:0;}
#messages { list-style-type: none; margin: 0; padding: 0 0 38px 0; }
#messages li { padding: 5px 10px;background-color: #EEE }
#messages li:nth-child(odd) { background-color: #FFF }
#lightbox{background-color: rgba(0,0,0,0.6); position: absolute; top: 0px; left: 0px; width:100%; height:100%;z-index:999;}
#nickname-input{text-align: center;margin: 20% 0px 0px;}
.user-nickname{font-weight: bolder; margin: 0 20px 0 0;}
#user-list{width: 20%;
height: 100%;
position: fixed;
top: 0px;
right: 0px;
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#ffffff+0,f3f3f3+50,ededed+100 */
background: #ffffff; /* Old browsers */
background: -moz-linear-gradient(top, #ffffff 0%, #f3f3f3 50%, #ededed 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, #ffffff 0%,#f3f3f3 50%,#ededed 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, #ffffff 0%,#f3f3f3 50%,#ededed 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 ); /* IE6-9 */
}
#user-list li{
list-style: none;
}
#user-list ul{
margin: 2rem 7rem;
}
#chat{width:80%;}
li{font-size: x-large;}
#new-message{
position: absolute;
bottom: 4.5%;
height: 10%;
text-align: center;
vertical-align: middle;
background: lightgreen;
width: 100%;
padding: 3rem;
display: none;
}
#get-list{
width:100%;
height: 4.5%;
background-color:#AE8BFF;
text-align:center;
padding: 0.8rem;
}
</style>
</head>
<body>
<div id="lightbox">
<div id="nickname-input">
<div id="nickname-error"></div>
<input id="nickname"/><input type="button" value="Name eingeben" onclick="addNickname()"/>
</div>
</div>
<div id="chat">
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Senden</button>
</form>
</div>
<div id="user-list">
<div id="get-list" onclick="getList()">Benutzerliste</div>
<ul id="list">
</ul>
<div id="new-message">Neue Nachricht</div>
<a href="/"><button id="logout">Logout</button></a>
</div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
/*
*
* Socket.io Chat von Christina Papadopoulos und Tim Schraml
*
* */
var socket = io();
localStorage.setItem("scroll","true");
/*
* Check for browser window height and enable the auto scroll
* */
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
localStorage.setItem("scroll","true");
document.getElementById("new-message").style.display = "none";
}else{
localStorage.setItem("scroll","false");
}
});
/*
* sends the chat message to the server as json and reset the chat input
* */
$('form').submit(function(){
var message = {"message": $('#m').val()};
socket.emit('chat message',JSON.stringify(message));
$('#m').val('');
return false;
});
/*
* socket listen to the server, on event "chat message".
* if on name is set(localStorage), the user dont get a message from the server.
* if auto scroll is enabled, the chat scrolls to the end of the document.
* else a message is displayed that says: "New message is arrived".
* */
socket.on('chat message', function(msg){
if(localStorage.getItem("nickname") != "") {
$('#messages').append($('<li>').html(msg));
}
if(localStorage.getItem("scroll") == "true"){
window.scrollTo(0, document.body.scrollHeight);
document.getElementById("new-message").style.display = "none";
}else{
document.getElementById("new-message").style.display = "block";
}
});
/*
* socket listen to the server on event "user list".
* gets from server a list of all user names(online)
* click on a name shows the whisper command in den chat input
* */
socket.on('user list', function(user){
document.getElementById("list").innerHTML = "";
for(var i = 0; i < user.length; i++){
$('#list').append($('<li>').html("<a onclick='whisperTo(this)'>"+user[i]+"</a>"));
}
});
/*
* socket listen to the server on event "nickname error".
* shows the user a message, if the nickname is already taken.
* */
socket.on('nickname error',function(msg){
document.getElementById("nickname-error").innerHTML = msg;
document.getElementById("lightbox").style.display = "block";
});
/*
* function add Nickname to the localStorage and close the lightbox.
* */
function addNickname(){
var nickname = document.getElementById("nickname").value;
localStorage.setItem("nickname",nickname);
document.getElementById("lightbox").style.display = "none";
socket.emit('login',localStorage.getItem("nickname"));
}
/*
* shows the whisper command in the chat input with nameObject inner text.
* */
function whisperTo(nameObject){
$('#m').val('/w "' + nameObject.innerText + '" ');
}
function getList(){
var message = {"message":"/list"};
socket.emit('chat message',JSON.stringify(message));
}
</script>
</body>
</html>