Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hook up actual messages from server component
- Loading branch information
David Becher
committed
Mar 7, 2013
1 parent
26e1915
commit bf3def1
Showing
8 changed files
with
139 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
<p class="chat-message-date">Today, 10:30AM</p> | ||
<li class="chat-message <%= (data.mine ? 'chat-message-mine' : '') %>" | ||
data-message-id="<%= data.cid %>"> | ||
<p class="chat-message-date"> | ||
<% var now = new Date(); %> | ||
<% if(data.date.getDate() === now.getDate() && data.date.getMonth() === now.getMonth() && data.date.getFullYear() === now.getFullYear()) { %> | ||
Today | ||
<% } else { %> | ||
<%= data.date.getMonth()+1 %>/<%= data.date.getDate() %>/<%= data.date.getFullYear() %> | ||
<% } %> | ||
<%= data.date.getHours() %>:<%= data.date.getMinutes() %> | ||
</p> | ||
<li class="chat-message <%= (data.mine ? 'chat-message-mine' : '') %>" > | ||
<h3 class="chat-message-user" ><%= data.user %></h3> | ||
<p class="chat-message-text"><%= data.message %></p> | ||
<p class="chat-message-text"><%= data.body %></p> | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Chatter library | ||
*/ | ||
|
||
(function(root) { | ||
|
||
var last_received_message = { | ||
timestamp: Date.now(), | ||
body: '', | ||
user: '' | ||
}; | ||
|
||
var last_sent_message = { | ||
timestamp: 0, | ||
body: '', | ||
user: '' | ||
}; | ||
|
||
function Client(host) {} | ||
|
||
Client.prototype.connect = function(host, fn, interval) { | ||
this.host = host; | ||
this.message_handler = fn; | ||
this.interval = interval || 500; | ||
this.listenForMessages(); | ||
}; | ||
|
||
Client.prototype.getRecentHistory = function() { | ||
// get initial transcript | ||
var self = this; | ||
$.get(self.host, function(data) { | ||
if (data) self.broadcast(data); | ||
}); | ||
}; | ||
|
||
Client.prototype.send = function(message, user, callback) { | ||
last_sent_message.body = message; | ||
last_sent_message.user = user || ""; | ||
$.post(this.host+'/message', {message:message, user:user}, function(data) { | ||
if (callback) callback(data); | ||
}); | ||
}; | ||
|
||
Client.prototype.broadcast = function(messages) { | ||
|
||
var lastOne = messages[messages.length - 1]; | ||
if (lastOne && lastOne.timestamp) last_received_message = lastOne; | ||
|
||
for(var i = 0, m; i < messages.length; i++) { | ||
m = messages[i]; | ||
// make sure new message isn't reflection of own message | ||
if (m.body != last_sent_message.body && m.user != last_sent_message.user) { | ||
this.message_handler(m); | ||
} | ||
} | ||
|
||
}; | ||
|
||
Client.prototype.listenForMessages = function() { | ||
var self = this; | ||
|
||
setTimeout(function() { | ||
$.get(self.host + "/since/" + last_received_message.timestamp, function(data) { | ||
if (data) self.broadcast(data); | ||
self.listenForMessages.call(self); | ||
}); | ||
}, self.interval); | ||
}; | ||
|
||
root.chatter = new Client(); | ||
|
||
})(window); |