最新写项目遇到一个功能,大概需求是用户a 发起任务给用户b,用户b及时提示告知。经过寻找资料,最终考虑使用pushlet。第一次使用先从简单学起,下面写一下初次使用心得:
pushlet百度百科解释: 是一个开源的 Comet 框架,Pushlet 使用了观察者模式:客户端发送请求,订阅感兴趣的事件;服务器端为每个客户端分配一个会话 ID 作为标记,事件源会把新产生的事件以多播的方式发送到订阅者的事件队列里。
工作原理:这位仁兄总结的不错:http://tomyz0223.iteye.com/blog/660738;
下面是我使用的步骤:
首先需要一个jar和一个js文件分别是:
ajax-pushlet-client.js
pushlet.jar
其次配置文件部分:
web.xml部分:
pushlet.properties 部分:
sources.properties部分:
最后是java代码部分:
前台页面部分:
最后 启动tomcat,在浏览器访问jsp页面就可以看到一个定时为1秒钟推送的结果啦!当然这里是很简单的使用pushlet,离我那个需求还差一点,今日太晚了,明天继续总结!
已有 0人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐
pushlet百度百科解释: 是一个开源的 Comet 框架,Pushlet 使用了观察者模式:客户端发送请求,订阅感兴趣的事件;服务器端为每个客户端分配一个会话 ID 作为标记,事件源会把新产生的事件以多播的方式发送到订阅者的事件队列里。
工作原理:这位仁兄总结的不错:http://tomyz0223.iteye.com/blog/660738;
下面是我使用的步骤:
首先需要一个jar和一个js文件分别是:
ajax-pushlet-client.js
pushlet.jar
其次配置文件部分:
web.xml部分:
<!-- pushlet --><servlet><servlet-name>pushlet</servlet-name><servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class><load-on-startup>1</load-on-startup></servlet> <servlet-mapping><servlet-name>pushlet</servlet-name><url-pattern>/pushlet.srv</url-pattern></servlet-mapping>
pushlet.properties 部分:
# # Pushlet configuration. # Place this file in the CLASSPATH (e.g. WEB-INF/classes) or directly under WEB-INF. # # $Id: pushlet.properties,v 1.13 2007/12/07 12:57:40 justb Exp $ # # # # config.version=1.0.2 # # CLASS FACTORY SPECIFICATION # # Change these if you want to override any of the core classes # within the Pushlet framework with your own custom classes. # # Examples: # - custom SessionManager for authorisation # - maintain lists of active subjects (topics) # - send events on subscription # - plug in custom logging like log4j # Note that you must maintain the semantics of each class ! # Below are the default properties for the core classes. controller.class=nl.justobjects.pushlet.core.Controller dispatcher.class=nl.justobjects.pushlet.core.Dispatcher logger.class=nl.justobjects.pushlet.util.Log4jLogger # logger.class=nl.justobjects.pushlet.util.DefaultLogger sessionmanager.class=nl.justobjects.pushlet.core.SessionManager session.class=nl.justobjects.pushlet.core.Session subscriber.class=nl.justobjects.pushlet.core.Subscriber subscription.class=nl.justobjects.pushlet.core.Subscription sessionmanager.class=com.cisoft.pushlet.MySessionManager # sessionmanager.maxsessions=200 # # DISPATCHER # # TODO: allow properties to be maintained in # a user dir # config.redirect=/etc/pushlet.properties # # LOGGING # # log level (trace(6) debug(5) info (4), warn(3), error(2), fatal(1)) # default is info(4) log.level=4 # # LOCAL EVENT SOURCES # # should local sources be loaded ? sources.activate=true # # SESSION # # algoritm to generate session key: # values: "randomstring" (default) or "uuid". # session.id.generation=uuid session.id.generation=randomstring # length of generated session key when using "randomstring" generation session.id.size=10 # Overall session lease time in minutes # Mainly used for clients that do not perform # listening, e.g. when publishing only. session.timeout.mins=5 # # EVENT QUEUE # # Properties for per-client data event queue # Size for queue.size=24 queue.read.timeout.millis=20000 queue.write.timeout.millis=20 # # LISTENING MODE # # You may force all clients to use pull mode # for scalability listen.force.pull.all=false # # Comma-separated list of User Agent substrings. # Force these browsers to use pull mode, since they # don't support JS streaming, matching is done using # String.indexOf() with lowercased agent strings # use multiple criteria with &. # listen.force.pull.agents=safari # # PULL MODE # # time server should wait on refresing pull client pull.refresh.timeout.millis=45000 # minimum/maximum wait time client should wait before refreshing # server provides a random time between these values pull.refresh.wait.min.millis=2000 pull.refresh.wait.max.millis=6000 # # POLL MODE # # time server should wait on refresing poll client poll.refresh.timeout.millis=60000 # minimum/maximum wait time client should wait before refreshing # server provides a random time between these values poll.refresh.wait.min.millis=6000 poll.refresh.wait.max.millis=10000
sources.properties部分:
# # Properties file for EventSource objects to be instantiated. # # Place this file in the CLASSPATH (e.g. WEB-INF/classes) or directly under WEB-INF. # # $Id: sources.properties,v 1.2 2007/11/10 14:12:16 justb Exp $ # # Each EventSource is defined as <key>=<classname> # 1. <key> should be unique within this file but may be any name # 2. <classname> is the full class name # # # Define Pull Sources here. These classes must be derived from # nl.justobjects.pushlet.core.EventPullSource # Inner classes are separated with a $ sign from the outer class. 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 #source7=com.cisoft.pushlet.PushletUtils$TaskPushlet #source1=com.cisoft.pushlet.MyPushletSource$MySource1 # TO BE DONE IN NEXT VERSION # define Push Sources here. These must implement the interface # nl.justobjects.pushlet.core.EventSource
最后是java代码部分:
package com.cisoft.pushlet; import java.io.Serializable; import nl.justobjects.pushlet.core.Event; import nl.justobjects.pushlet.core.EventPullSource; public class MyPushletSource implements Serializable{ /** * */ private static final long serialVersionUID = 1L; public static class MySource1 extends EventPullSource { @Override protected long getSleepTime() { //定时 1秒向客户端 推送一次 return 1000; } @Override protected Event pullEvent() { Event event =Event.createDataEvent("myevent1"); event.setField("key1","my_value1"); return event; } } }
前台页面部分:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript" src="js/pushlet/ajax-pushlet-client.js"></script> <script type="text/javascript"> PL.userId='piero'; PL._init(); PL.joinListen('myevent1'); function onData(event) { alert(event.get("key1")); } </script> </head><body></body></html>
最后 启动tomcat,在浏览器访问jsp页面就可以看到一个定时为1秒钟推送的结果啦!当然这里是很简单的使用pushlet,离我那个需求还差一点,今日太晚了,明天继续总结!
已有 0人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐