版权声明:本文为博主牟云飞原创文章。未经博主同意不得转载。
https://blog.csdn.net/myfmyfmyfmyf/article/details/36364337
---------------------在jsp页面中增加----------------------
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>">
//从官网下载
<script type="text/javascript" src="<%=path%>/cc/util/js/ajax-pushlet-client.js"></script>
<script type="text/javascript">
PL._init();
//监听mutiSend时间。mutiSend是我定义的
PL.joinListen('/mutiSend');
function onData(event) {
try{
//读取内容
var context=event.get("msg");
//解决汉字乱码问题
context=decodeURIComponent(context);
if(null!=context&&"undefined"!=context){
//var a=document.getElementById("contex");
//a.innerHTML=context;
pushInfoShowDiv(context);
}
}catch(e){
}
// 离开
// PL.leave();
}
</script>
--------------------------web.xml文件里增加
<!-- pushlet -->
<servlet>
<servlet-name>pushlet</servlet-name>
<servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pushlet</servlet-name>
<url-pattern>/pushlet.srv</url-pattern>
</servlet-mapping>
------------------------java文件-----------------
//向全部和****名称匹配的事件进行推送
public void mutiSend(){
try {
//获得数据
List<RemindInfo> list = retrieve(null,null,null,null,null);
String remindContent="";
if(null!=list&&0!=list.size()){
remindContent=list.get(0).getRemindContent();
}
Event event =Event.createDataEvent("/mutiSend");
event.setField("msg", URLEncoder.encode(remindContent+"<br/>推送时间:"+new Date().toLocaleString(),"UTF-8"));//解决乱码
Dispatcher.getInstance().multicast(event);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
假设须要页面点击发送,然后推送给各个客户端。在action或者servlet中增加
Event event =Event.createDataEvent("/mutiSend");
event.setField("msg", URLEncoder.encode(remindContent+"<br/>推送时间:"+new Date().toLocaleString(),"UTF-8"));//解决乱码
Dispatcher.getInstance().multicast(event);
这三句代码就可以
------------效果-------------