-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.js
43 lines (36 loc) · 1.13 KB
/
chatserver.js
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
Messages = new Meteor.Collection("messages");
if (Meteor.is_client) {
Template.chat.message = Messages.find();
Template.chat.displayTime = function(timestamp) {
if (timestamp === undefined) {
return "Past";
} else {
var d = new Date(timestamp);
function pad(n){return n<10 ? '0'+n : n};
return "" + pad(d.getHours()) +
":" + pad(d.getMinutes()) +
":" + pad(d.getSeconds());
}
}
Template.chat.events = {"DOMNodeInserted": function(evt) {
// This gets called MANY times, on update, so it's a bit non-optimal.
// Still, it's the best solution I've found so far :/
// Meteor.flush();
// setTimeout(function () {
$(".chat").scrollTop(100000);
}};
Template.input.events = { 'keydown': function (evt) {
if (evt.which === 13) {
Messages.insert({name: $("#name_input").val(),
text: $("#chat_input").val(),
timestamp: (new Date()).getTime()});
$("#chat_input").val("");
}
}
}
}
if (Meteor.is_server) {
Meteor.startup(function () {
// Initial messages here
});
}