-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
180 lines (149 loc) · 4.6 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
<title>Quickchat</title>
<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- pubnub -->
<script src="http://cdn.pubnub.com/pubnub.min.js"></script>
<!-- jquery -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- bootstrap -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- fontawesome -->
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<style type="text/css">
.fa {
margin-right: 4px;
}
</style>
<script type="text/javascript">
var username;
var pubnub;
$(document).ready(function()
{
// when the user enters in their username
$('#enterform').submit(function (e)
{
// prevents the page from refreshing
e.preventDefault();
// check for a username
if ($('#username').val() != '')
{
// assigns the username variable to what was typed into the form
username = $('#username').val();
// clear the username field
$('#username').val('');
} else {
// assign a generic username - this is not a good way to do this, btw
username = 'Anonymous';
}
// show the chatroom div
$('#enterscreen').hide();
$('#chatscreen').show();
// initialize pubnub (replace the keys below with your own)
pubnub = PUBNUB.init(
{
publish_key : 'your_publish_key',
subscribe_key : 'your_subscribe_key',
uuid: username
});
// subscribe to a chatroom channel
pubnub.subscribe(
{
channel : "chat",
message: handleMessage,
presence: function(data)
{
// when a user joins the chatroom (requires presence to be turned on)
if(data.action == "join")
{
$('#users').append($('<p id="' + data.uuid + '">' + '<i class="fa fa-user text-primary"></i><span class="text-muted">' + data.uuid + '</span></p>'));
}
// when a user leaves the chatroom (requires presence to be turned on)
if(data.action == "leave" || data.action == "timeout")
{
$('#' + data.uuid).remove();
}
}
});
});
// when a user posts a chat message
$('#chatform').submit(function(e)
{
// prevents the page from refreshing
e.preventDefault();
// if the message isn't empty
if ($('#message').val() != '')
{
// publish the message to everyone in the chatroom
pubnub.publish(
{
channel: 'chat',
message: {
username: username,
text: $('#message').val()
}
});
// clear the message field
$('#message').val('');
}
});
});
// when a message is received
function handleMessage(message)
{
// add the message to the messages div
$('#messages').append('<p>' + '<span class="text-primary"><i class="fa fa-comment"></i>' + message.username + ':</span> <span class="text-muted">' + message.text + '</span></p>');
// automatically scroll the div
$('#messages').scrollTop(250);
};
</script>
</head>
<body>
<div class="container">
<div class="header">
<h2 class="text-primary" style="padding-bottom:15px"><i class="fa fa-comments-o fa-lg"></i>Schnell mal miteinander schreiben!</h2>
</div>
<!-- enter username form -->
<div id="enterscreen">
<form id="enterform">
<div class="form-group">
<input id="username" name="username" type="text" placeholder="Nickname wählen!" />
</div>
<input id="enterbtn" type="submit" class="btn btn-primary" value="Los" />
</form>
</div>
<!-- chatroom -->
<div id="chatscreen" style="display: none">
<!-- message form -->
<form id="chatform">
<div class="form-group">
<input id="message" name="message" type="text" placeholder="Was willst du schreiben?" />
</div>
<input id="chatbtn" type="submit" class="btn btn-primary" value="Senden" />
</form>
<br /><br />
<!-- display messages -->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Nachrichten</h3>
</div>
<div id="messages" class="panel-body">
</div>
</div>
<!-- display users -->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Benutzer*innen</h3>
</div>
<div id="users" class="panel-body">
</div>
</div>
</div>
</div>
</body>
</html>