Pushlet个人写的源码示例,附加官方示例

在ajax-pushlet-client.js文件中找到以下代码:
_getWebRoot: function() {
/** Return directory of this relative to document URL. */
if (PL.webRoot != null) {
return PL.webRoot;
}
//derive the baseDir value by looking for the script tag that loaded this file
var head = document.getElementsByTagName('head')[0];
var nodes = head.childNodes;
for (var i = 0; i < nodes.length; ++i) {
var src = nodes.item(i).src;
if (src) {
[color=orange] var index = src.indexOf("ajax-pushlet-client.js");
if (index >= 0) {
index = src.indexOf("lib");[/color]
PL.webRoot = src.substring(0, index);
break;
}
}
}
return PL.webRoot;
},
本函数,有的版本是这样写的:


_getWebRoot: function() {
/** Return directory of this relative to document URL. */
if (PL.webRoot != null) {
return PL.webRoot;
}
//derive the baseDir value by looking for the script tag that loaded this file
var head = document.getElementsByTagName('head')[0];
var nodes = head.childNodes;
for (var i = 0; i < nodes.length; ++i) {
var src = nodes.item(i).src;
if (src) {
[color=red]var index = src.indexOf("lib/ajax-pushlet-client.js");
[/color] if (index >= 0) {
PL.webRoot = src.substring(0, index);
break;
}
}
}
return PL.webRoot;
},


改成用橙色写的就可以了。
[color=red]
还有找不到pushlet.properties文件,把文件放在src根目录下,就可以了,不要放在WEB-INF下。[/color]


[color=red]java相关代码:[/color]
package com.example.demo;
public class HelloWorldPlushlet extends EventPullSource implements Serializable{

/**
*
*/
private static final long serialVersionUID = 4387401648231574196L;
// 休眠五秒
@Override
protected long getSleepTime() {
return 5000;
}
@Override
protected Event pullEvent() {
Event event = Event.createDataEvent("[color=red]/你的监听事件以'/'开头[/color]");
event.setField("mess", "hello,world!Plushlet!");
return event;
}


}

[color=red]sources.properties文件内容:[/color]

#类的全路径名
source7=com.example.demo.HelloWorldPlushlet

[color=red]pushlet.properties文件内空:[/color]


#
# 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.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


[color=red]jsp页面内容:[/color]


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Pragma" content="no-cache" />
<script type="text/javascript" src="ajax-pushlet-client.js"></script>
<script type="text/javascript">
PL._init();
PL.joinListen('[color=red]你的监听事件以'/'开头[/color]');
function onData(event) {
alert(event.get("mess"));
// 离开
// PL.leave();
}
</script>
</head>
<body>
<center>
<h1>
my first pushlet!
</h1>
</center>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值