爲什麼會有這篇文章呢??
由於最近需要用到服務器推送技術,瞭解了一下這方面的知識,查找了好多例子,綜合考慮使用Comet4j,不過目前這款插件只支持tomcat(按照tomcat的版本需要下載對應的jar包)。
準備工作
1.下載自己對應的版本的jar tomcat6 -----comet4j-tomcat6.jar tomcat7------comet4j-tomcat7.jar
2.下載comet4j.js
3、由於comet4j基於NIO實現,所以需要修改tomcat的server.xml中的配置見下圖
將原先的HTTP/1.1改爲org.apache.coyote.http11.Http11NioProtocol以支持NIO,這樣第一步的準備工作就完成了。
下面 我們準備客戶端代碼
<html>
<head>
<title>論壇</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="${ctx }/yogurtResource/assembly/comet/comet4j.js"></script>
<script type="text/javascript">
$(document).ready(function(){
init();
});
function init(){
var forum_list = $("#forum_list");
JS.Engine.start('../comet');
JS.Engine.on(
'hello',function(text){
forum_list.html(text);
var html = "<span id='forum_list8'></span>"
$("#list").html(html);
$("#forum_list8").html("我是測是者a");
});
JS.Engine.on('start',function(cId, channelList, engine){
$("#forum_list3").html(cId);
});
}
function addForum(){
$.ajax({
url:'${ctx }/forum/add_forum',
data:{'hello':'hello'},
type:"post",
dataType:"json",
success:function(data){
// alert("異步成功");
},error:function(){
alert("錯誤!!");
}
});
}
</script>
</head>
<body>
這是論壇頁面<br><br><br><br><br><br>
<div>
<form id="addForum" action="">
<input type="text" name="channel" value="hello"/></form>
<button id="forum" onclick="addForum();">模擬論壇提交</button>
</div>
<div id="list">
<span id="forum_list"></span><br>
<span id="forum_list2"></span>
<span id="forum_list3"></span>
</div>
</body>
</html>
下面我們準備服務端代碼
@Controller("forumController")
@RequestMapping("/forum")
public class ForumController extends BaseController{
@RequestMapping(value = "/forum_list")
public ModelAndView forumList(){
return new ModelAndView("forum/forum_list");
}
@RequestMapping(value="/add_forum")
@ResponseBody
public Object addForum(){
String abc="我是向前臺推送的消息";
User user=new User();
user.setUsername("偉大的天朝");
// 創建CometEngine實體類
CometEngine engine = CometContext.getInstance().getEngine();
engine.sendToAll("hello", user.getUsername());
return user;
}
}
還需要一個監聽來創建信道
public class ChannelComet implements ServletContextListener{
private static final String CHANNEL = "hello";
public void contextInitialized(ServletContextEvent arg0) {
// CometContext是一個單例靜態
System.out.println("-----------------------------------------------------------------------------------------");
CometContext cc = CometContext.getInstance();
cc.registChannel(CHANNEL);// 註冊應用的channel
System.out.println("註冊信道成功");
}
public void contextDestroyed(ServletContextEvent arg0) {
}
}
最後來完成我們的配置工作
<!-- comet4j -->
<listener>
<description>Comet4J容器偵聽</description>
<listener-class>org.comet4j.core.CometAppListener</listener-class>
</listener>
<servlet>
<description>Comet連接[默認:org.comet4j.core.CometServlet]</description>
<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>/comet</url-pattern>
</servlet-mapping>
<listener>
<description>ChannelComet</description>
<listener-class>ChannelComet</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
注意:
1、此處不再贅述API中的各個類的具體作用及功能,請自己網絡查找(天朝的網絡上我是找不到API,就不附贈 了)其他的jar包,js已上傳comet4j請下載
2、這裏面的配置是有順序要求的,真正的項目中肯定不只是這麼幾個配置項,配置順序不對會導致無法響應後臺推送到前臺的消息
3、監聽要換成咱麼自己的監聽類
4、CometServlet的url-pattern配置和前臺的start項中的要一致