使用Pushlet将消息从服务器端推送到客户端
来源:互联网 发布:阿里手机小号软件 编辑:程序博客网 时间:2024/04/29 10:58
使用Pushlet来实现服务器端向客户端推送信息
1. 实现方式:
有两种实现方式:
1.
2.
以下分别给予介绍。
2. 特别注意
在开始测试之前,有三点非常重要,需要实现讲明,否则程序将会无法正常运行:
2.1. JSP页面上的设定
JSP页面上必须添加以下代码以确保Pushlet能够正确的获得后台服务的地址:
<base href="<%=request.getContextPath()%>">
2.2. Pushlet的JS文件的Bug修改
需要修改被引用的JS文件ajax-pushlet-client.js的内容,找到
PL.pushletURL = PL._getWebRoot() + 'pushlet.srv';
将其修改为
PL.pushletURL = 'pushlet.srv';
修改的原因是Pushlet进行地址解析的方法在某些应用中会解析错误,导致请求的路径是nullpushlet.srv?,最终导致无法正确的请求到服务器的信息。
2.3. 中文问题
一般情况下,如果不做特殊处理,中文问题将会导致Pushlet的客户端停止响应,解决办法是,在使用Pushlet的客户端代码发送消息之前,将其进行转码,代码为
encodeURIComponent( msg)
3. 正式开始
以上准备工作完毕,就可以正式的开发测试样例了。
3.1. 定时的从后台向前台push信息
(1)
(2)
序号
文件名
源位置
目标位置
备注
1.
pushlet.jar
{pushlet-2.0.4}\lib
项目类路径
如果使用的是applet的话,还需要将pushletclient.jar设置到项目的类路径中去
2.
log4j.properties
pushlet.properties
sources.properties
{pushlet-2.0.4}\webapps\pushlet\WEB-INF\classes
项目的src根路径
注意稍后需要修改sources.properties,其他两个文件的内容不需要修改
3.
ajax-pushlet-client.js
{pushlet-2.0.4}\webapps\pushlet\lib
项目的webroot\lib
需要按照之前的描述修改其中的内容
(3)
(4)
package com.guoguo;
import java.io.Serializable;
import java.io.UnsupportedEncodingExcep
import sun.rmi.runtime.Log;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
public class HelloWorldPushlet implements Serializable {
}
(5)
source7=com.guoguo.HelloWorldPushlet$HWPushlet
以上方式适用于有内部类的情况,如果没有内部类的话,使用以下的方式进行注册(这时外部类必须继承父类EventPullSource)
source7=com.guoguo.HelloWorldPushlet
(6)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!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=ISO-8859-1">
<title>Pushlet Test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<base href="<%=request.getContextPath()%>">
</head>
<body>
</body>
</html>
(7)
3.2. 主动控制发送消息
3.2.1. 有刷新的提交信息(服务器端主动发送消息)
(1)
Servlet代码
package com.guoguo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.justobjects.pushlet.core.Dispatcher;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.SessionManager;
public class ChatServlet extends HttpServlet {
}
Web.xml
(2)
发送端
<base href="<%=request.getContextPath()%>">
<form action="<%=request.getContextPath()%>/ChatServlet">
</form>
接收端
<base href="<%=request.getContextPath()%>">
<script type="text/javascript" src="lib/ajax-pushlet-client.js"></script>
<div id="guoguo"></div>
<script type="text/javascript">
</script>
启动服务器,从发送端提交信息,内容会在接收端显示出来
3.2.2. 无刷新的提交信息(从客户端发送消息)
发送端
<base href="<%=request.getContextPath()%>">
<script type="text/javascript" src="lib/ajax-pushlet-client.js"></script>
<script type="text/javascript">
</script>
<input type="text" name="mymsg">
<input type = "button" value="发消息" onclick="sendnews(mymsg.value)"/>
接收端
<base href="<%=request.getContextPath()%>">
<script type="text/javascript" src="lib/ajax-pushlet-client.js"></script>
<div id="guoguo"></div>
<script type="text/javascript">
</script>
启动服务器,从发送端提交信息,内容会在接收端显示出来
- 使用Pushlet将消息从服务器端推送到客户端
- 使用Pushlet将消息从服务器端推送到客户端【
- 使用Pushlet将消息从服务器端推送到客户端【原创】
- 使用Pushlet将消息从服务器端推送到客户端
- 使用Pushlet将消息从服务器端推送到客户端
- 使用Pushlet将消息从服务器端推送到客户端【原创】
- 开源框架Pushlet入门,使用Pushlet将消息从服务器端推送到客户端
- Pushlet实现消息从服务器端推送到客户端
- 使用Pushlet来实现服务器端向客户端推送信息
- 使用Pushlet来实现服务器端向客户端推送信息
- 使用pushlet实现消息服务端推送至客户端
- 使用pushlet实现消息服务端推送至客户端
- Openfire服务器端推送消息到客户端
- pushlet 服务器主动推送数据到客户端
- pushlet 服务器数据推送到客户端
- pushlet实现服务端推送消息到浏览器
- pushlet实现服务端推送消息到浏览器
- Pushlet之消息通知- 实现服务器端推送技术
- 学习英语音标,坚持
- 深入理解C语言-09 编译器介绍
- Activity、Window和View三者间的关系【Android】
- 题目1199:找位置
- Android开发:shape和selector和layer-list的(详细说明)
- 使用Pushlet将消息从服务器端推送到客户端
- sealed关键字——密封类
- JSP Cookie使用方法详解
- 黑马程序员_OC基础10_内存管理(多对象)
- Win flex-bison 的简单使用
- 经典智力题:飞机加油问题
- HTML5画布的实时画图
- java 正则表达式
- [cocos2d-x] --- 完美解决中文乱码(测试通过)