读书人

服务器推送PushLet的施用lt;一gt

发布时间: 2012-08-25 10:06:20 作者: rapoo

服务器推送——PushLet的应用<一>

?? 背景 :最近,公司需要做一个设备监控方面的项目,前台采用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;@Overrideprotected 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$TemperatureEventPullSourcesource2=nl.justobjects.pushlet.test.TestEventPullSources$SystemStatusEventPullSourcesource3=nl.justobjects.pushlet.test.TestEventPullSources$PushletStatusEventPullSourcesource4=nl.justobjects.pushlet.test.TestEventPullSources$AEXStocksEventPullSourcesource5=nl.justobjects.pushlet.test.TestEventPullSources$WebPresentationEventPullSourcesource6=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 、由于知识面、技术能力有限,内容主要面向应用、没有过多关于原理性的介绍。

??????????????? 另外文中阐述若有?? 不 ( 够 ) 正确之处,望广大网友“友好”指正。

?

1 楼 crazy_cf 2012-06-12 楼主! 怎么似曾相识呀

读书人网 >编程

热点推荐