zsz516的gravatar头像
zsz516 2014-07-21 16:27:41

Java新消息提醒

类似人人网那种,如果有人给你评论或者回复,就在状态栏那里闪烁“1”或者“1条新消息”这样的

所有回答列表(5)
yongfahuang的gravatar头像
yongfahuang  LV15 2014年7月21日

长轮询方式搞得吧,或者websocket

dfrank的gravatar头像
dfrank  LV4 2014年7月21日

添加一个异步接口,返回新消息的条数,每隔X秒请求一次。

最代码官方的gravatar头像
最代码官方  LV167 2014年7月21日

最代码是轮询的方式实现的。

当用户a评论用户b的分享的时候,给用户b的私信数+1。

js去轮询3秒获取该用户的私信数。

js代码如下:

remind = function(uuid,uid) {
			$.ajax({
				url : "/user/reminds.htm?uuid="+uuid+"&uid="+uid,
				type : 'GET',
				dataType : 'json',
				success : function(json) {
					if(json.error!=""){
						return;
					}
					if(json.remindDescs.length==0){
						return;
					}
					var template = $('#remind_template').html();
					var html = Mustache.to_html(template, json).replace(/^\s*/mg, '');
					$("#reminds").html("").append(html);
					if($("title").html()==""){
						$("title").html("您有新的提醒");
					}else{
						$("title").html("");
					}
				}
			});
		};

调用端代码:

setInterval("remind(1550463811307520,1)",10000);

 

终极拾荒者☆风之精灵★巽的gravatar头像
终极拾荒者☆风之精灵★巽  LV7 2014年7月22日

我想说一个,轮询方式就显得太烂了,如果用户量多了,你的服务器要承受得住轮询。

dwr3实现消息精确推送,DWR Reverse Ajax,这是一个轻量级框架,使用方便。

详细请看文章:http://www.blogjava.net/stevenjohn/archive/2012/07/07/382447.html

如有疑问,QQ:876292931,欢迎探讨,已有现成测试的源码。

@最代码官方:有时候我总觉得最代码开一段时间后会很看,就感觉是轮询,其实很不好。

masque的gravatar头像
masque  LV16 2014年7月22日

websocket

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友