最近对比了几种消息推送的方案:
xmpp(openfire)——对消息服务器的要求比较高,没有找到合适的java实现方案;
极光推送、百度推送——自动生成的id,不好绑定本地用户,作为公告类的消息推送可以,但作为本地用户间的消息推送机制还是不太合适;
comet4j——能实现消息推送,但是每个页面保持连接的js对性能的消耗也厉害,部分代码配置如下:
jsp页面:
<script type="text/javascript" src="${ctx }/resources/js/comet4j.js"></script> <script type="text/javascript"> function init() { var kbDom1 = document.getElementById(‘kbb‘); JS.Engine.on( { hello : function(kb) {//侦听一个channel var strs = new Array(); //定义一数组 strs = kb.split(","); //字符分割 for (i = 0; i < strs.length; i++) { if (strs[i] == kbDom1.value && strs[i] != ‘‘) { document.getElementById(‘toubiao‘).style.display = ""; showUnReadMsgCount(); } } } }); JS.Engine.start(‘conn‘); JS.Engine.on(‘start‘, function(cId, channelList, engine) { }); } init(); </script> <script> function showUnReadMsgCount(){ if($("#isLogin").val() === "true"){ $.ajax( { url : ctx + "/admin/getUnReadMsgCount.do", type : "post", dataType : "json", success : function(data) { var count = data[0]; if(count == 0){ return; } $("#unReadCount").html("<span></span><a href=‘"+ctx+"/admin/receiveList.do‘ style=‘font-weight:700;‘>未读消息<label>"+count+"</label></a>"); } }); } }; </script>
web.xml中配置
<!-- comet4j start <listener> <listener-class>org.comet4j.core.CometAppListener</listener-class> </listener> <servlet> <display-name>CometServlet</display-name> <servlet-name>CometServlet</servlet-name> <servlet-class>org.comet4j.core.CometServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CometServlet</servlet-name> <url-pattern>/conn</url-pattern> </servlet-mapping> <listener> <description>Comet4jMsgListener</description> <listener-class>com.tenchong.util.Comet4jMsgListener</listener-class> </listener> comet4j end -->