背景 :最近,公司需要做一个设备监控方面的项目,前台采用ExtJs、后台采用SSH框架+SNMP技术实现。但是由于设备监控大多数时候都需要后台实时的与页面进行信息传递,平时只做过前台主动向后台发送请求,然后获取信息的,而从后台主动向前台发送消息的还没做过。只好问度娘、谷哥,知道了“服务器推送技术”,进而知道了PushLet这个东东,然后在网上开始扒资料,但是网上的资料都差不多(你们懂的),例子普遍的都是类似于IM即时聊天的实现。虽然如此,这些资料依然给了我莫大的帮助,在此非常感谢那些资料的贡献者(无论是原创还是转载者^_^)!
好了,废话少说,翠花,上例子!
Step1 首先当然要获取pushlet开发包
http://sourceforge.net/projects/pushlets/files/pushlets/2.0.4/pushlet-2.0.4.zip/download
下载后解压,获取lib下的pushlet.jar pushletclient.jar 、以及webapps\pushlet\WEB-INF\classes下的 log4j.properties pushlet.properties sources.properties。
Step-2 在src目录中添加 log4j.properties、pushlet.properties、 sources.properties ,待会儿需要对sources.properties进行修改和配置。
Step-3 配置Web.xml 在Web.xml中加入如下内容:
<servlet>
<servlet-name>pushlet</servlet-name>
<servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Define the Servlet Mappings. -->
<!-- The pushlet -->
<servlet-mapping>
<servlet-name>pushlet</servlet-name>
<url-pattern>/pushlet.srv</url-pattern>
</servlet-mapping>
Step-4 创建自己的事件源
package com.fornew;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
public class PushLet implements Serializable{
private static final long serialVersionUID = 1L;
static public class PushEvent extends EventPullSource{
public int i = 1;
@Override
protected long getSleepTime() {
return 3000; //设置休眠时间(getSleepTime)
}
@Override
//创建事件(pullEvent)其中,业务部分就写在pullEvent()方法中即可,这个方法会被定时调用。
protected Event pullEvent() {
Event event =Event.createDataEvent("/fornew/push"); //事件与jsp页面"绑定"
StringBuffer str = new StringBuffer("富二代【").append(i++).append('】');
str.append(this.getRandomStr());
try {
//转码,否则中文在页面出现乱码,且会使页面“掉线”
event.setField("hw",new String(str.toString().getBytes("UTF-8"), "ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return event;
}
//产生随机数,便于页面观察
public String getRandomStr(){
Random random = new Random();
return Math.abs(random.nextInt())+"";
}
}
}
Step-5 在上面加入的sources.properties中配置事件源
原配置如下(不是html代码哈):
source1=nl.justobjects.pushlet.test.TestEventPullSources$TemperatureEventPullSource
source2=nl.justobjects.pushlet.test.TestEventPullSources$SystemStatusEventPullSource
source3=nl.justobjects.pushlet.test.TestEventPullSources$PushletStatusEventPullSource
source4=nl.justobjects.pushlet.test.TestEventPullSources$AEXStocksEventPullSource
source5=nl.justobjects.pushlet.test.TestEventPullSources$WebPresentationEventPullSource
source6=nl.justobjects.pushlet.test.TestEventPullSources$PingEventPullSource
将其删除或者注释掉,加入自己的事件源,如下 (不是html代码哈) :
source1=com.fornew.PushLet$PushEvent
Step-6 编写index.jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>PushLet-示例</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<script type="text/javascript" src="pushlet/ajax-pushlet-client.js"></script>
<script type="text/javascript">
PL._init();
PL.joinListen('/fornew/push');
function onData(event){
var str = event.get("hw");
var elId = document.getElementById('wxh');
//产生随机颜色值
var getColor = function(){
var r = Math.ceil(Math.random()*256).toString(16);
r = r.length==1?'0'+r:r;
return r;
}
var color = '#';
color =(color + getColor() + getColor() + getColor());
//产生随机颜色值,便于观察
elId.value = str;
elId.style.color=color;
}
</script>
</head>
<body>
<div>
<input type="text" id="wxh" size="50" name="wxh" maxlength="80" value="500"/>
</div>
</body>
</html>
注意: 需要引入pushlet的js脚本(ajax-pushlet-client.js)
其中比较重要的是这段JavaScript代码,PL._init()、PL.joinListen("")为固定用法,必须写,且顺序固定。joinListen中的“字符串”与自定义事件源中的createDataEvent中的“字符串”相对应另外function onData(event)方法也为固定用法、为pushlet脚本自定义页面响应函数。
<script type="text/javascript">
PL._init();
PL.joinListen('/fornew/push');
function onData(event){
var str = event.get("hw");
var elId = document.getElementById('wxh');
//产生随机颜色值
var getColor = function(){
var r = Math.ceil(Math.random()*256).toString(16);
r = r.length==1?'0'+r:r;
return r;
}
var color = '#';
color =(color + getColor() + getColor() + getColor());
//产生随机颜色值
elId.value = str;
elId.style.color=color;
}
</script>
自此,编译、部署项目,在浏览器中访问该项目,后台Java程序每隔3秒将会向index.jsp推送一次信息。
参考: http://edwin492.iteye.com/blog/1124751
个人说明:
1 、为表示对相关资料贡献者的尊重,转载、参考内容均有特殊说明并附出处【 URL 】
2 、由于知识面、技术能力有限,内容主要面向应用、没有过多关于原理性的介绍。
另外文中阐述若有 不 ( 够 ) 正确之处,望广大网友“友好”指正。
分享到:
相关推荐
【服务器推送技术——PushLet应用详解】 在现代网络应用中,服务器推送技术(Server-Sent Events, SSE 或 WebSockets)已经成为实时通信的关键组件。本文将深入探讨PushLet的使用,这是一种实现服务器推送的框架,...
在IBM于2008年发布的技术报告中,详细介绍了服务器推送技术——即所谓的Pushlet。这项技术为Web应用程序的发展带来了革命性的变化,尤其是在异步通信方面。本文将根据报告内容深入探讨Pushlet的相关知识点,包括其...
在最新的版本 2.0.4 中,该框架引入了一个重要的新特性——支持指定用户推送,这使得消息传输更加精细化,提升了用户体验,同时也优化了服务器资源的利用。 一、Pushlet 框架基础 Pushlet 框架基于 publish-...
3. **数据解析**:接收到服务器推送的数据后,客户端需要解析这些数据,通常是JSON格式,然后进行渲染或处理。 **Pushlet的应用场景** 1. **实时通知**:例如,即时消息系统或邮件通知,服务器可以直接将新消息推送...
Pushlet是一个基于Java的轻量级推送技术框架,常用于构建实时Web应用程序,尤其是即时通讯系统。 Pushlet的核心思想是服务器向客户端推送数据,而不是传统的客户端轮询方式。这种设计模式提高了效率,减少了网络...