首页 > 代码库 > pushlet服务端推送

pushlet服务端推送

---------------------在jsp页面中加入---------------------- 

 

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href=http://www.mamicode.com/"">
//从官网下载
<script type="text/javascript" src=http://www.mamicode.com/"<%=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);

这三句代码即可

 

------------效果-------------