Skip to content

Instantly share code, notes, and snippets.

@hscstudio
Created January 23, 2016 08:07
Show Gist options
  • Save hscstudio/b5edf84b025ef8c8e875 to your computer and use it in GitHub Desktop.
Save hscstudio/b5edf84b025ef8c8e875 to your computer and use it in GitHub Desktop.
<script type="text/javascript">jQuery(document).ready(function () {
if(window.EventSource !== undefined){
var es = new EventSource("/message/get-message");
es.addEventListener("message", function(e) {
var data = JSON.parse(e.data);
// GET BADGE MESSAGE
var selector = "li.messages-menu > a > span";
if ($(selector).length && data.count>0) {
$(selector).html(data.count)
}
else{
var selector = "li.messages-menu > a";
var content = $(selector).html();
$("li.messages-menu > a").html(
content+"<span class=\"label label-success\">"+data.count+"</span>"
)
}
var selector = "li.messages-menu > ul.dropdown-menu > li:first-of-type";
$(selector).html("You have " + data.count + " messages")
if(data.count>0){
// GET LIST MESSAGE
var selector = "li.messages-menu > ul.dropdown-menu > li:first-of-type + li > div > ul";
subjects = data.subjects.split("|");
urls = data.urls.split("|");
times = data.times.split("|");
photos = data.photos.split("|");
$(selector).html("");
for(var i = 0; i < subjects.length; i++)
{
photo = "<div class=\"pull-left\"><img src=\""+photos[i]+"\" class=\"img-circle\" alt=\"user\"></div>";
time = " <i class=\"fa fa-clock-o\"></i> <span data-livestamp=\""+times[i]+"\"></span>";
$(selector).append("<li><a href='"+urls[i]+"'>"+photo+"<h4>"+subjects[i]+"<small>"+time+"</small></h4></a></li>")
}
}
}, false);
}
else {
(function poll(){
setTimeout(function(){
$.ajax({ url: "/message/test",
success: function(data){
//alert(data);
poll();
},
error: function (data, status, errorThrown) {
alert(status + ">> " + errorThrown);
}
//dataType: "json"
});
}, 3000);
})();
}
});</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment