Skip to content

Instantly share code, notes, and snippets.

@chrisallick
Created October 15, 2012 18:11
Show Gist options
  • Save chrisallick/3894094 to your computer and use it in GitHub Desktop.
Save chrisallick/3894094 to your computer and use it in GitHub Desktop.
Make client doing annoying work of calculating their time, but make server do important task of knowing when to send timestamp.
RUBY:
# push timecheck command to clients every 30 minutes for rooms where client count is greater than 0
timecheck = EM.add_periodic_timer(60*30) {
@channels.keys.each do | key |
if @channels[key][:clients].length > 0
data = { :type => "roomnotice", :special => "timecheck", :msg => "heyo!" }.to_json
@channels[key][:channel].push data
end
end
}
JAVASCRIPT:
var months=new Array();
months[0]="Jan";
months[1]="Feb";
months[2]="Mar";
months[3]="Apr";
months[4]="May";
months[5]="Jun";
months[6]="Jul";
months[7]="Aug";
months[8]="Sep";
months[9]="Oct";
months[10]="Nov";
months[11]="Dec";
timestamp = function() {
var date = new Date();
var hours = date.getHours()%12;
var minutes = "";
var ampm = "";
var month = months[date.getMonth()];
var day = date.getDate();
var year = date.getUTCFullYear();
// am or pm
if( date.getHours() < 12 ) {
ampm = "am";
} else {
ampm = "pm";
}
if( hours == "0" ) { hours = "12"; }
// set minutes
if( date.getMinutes() < 10 ) {
minutes = "0"+date.getMinutes();
} else {
minutes = date.getMinutes();
}
var time = month + " " + day + ", " + year + ", " + hours+":"+minutes+ampm;
// var msg = trash.createMessage( "roomnotice", time );
// msg["special"] = "timecheck";
//trash.trashio.sendMessage( msg );
// $("#chat").append("<p class='chatitem roomnotice time'>"+time+"</p>");
// $("#chat").animate({
// scrollTop: $("#chat")[0].scrollHeight
// });
return time;
}
// when you receive the timecheck command:
$("#chat").append("<p class='chatitem roomnotice time'>"+timestamp()+"</p>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment