1后台
package test.controller;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
public class HelloWorldPushlet extends EventPullSource implements Serializable {
private static final long serialVersionUID = -4378845831200885879L;
/**
* 推送时间间隔
*/
protected long getSleepTime() {
return 1000;
}
/**
* 推送时所触发的方法
*/
protected Event pullEvent() {
//创建事件,并制定事件的主题
Event event = Event.createDataEvent("/fxh/helloworld");
String data = "hello world,microbingbing" + System.currentTimeMillis();
try {
data=new String(data.getBytes("UTF-8"),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
event.setField("message", data);
return event;
}
}
2sources.properties
source3=test.HelloWorldPushlet
3页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>抽取记录</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${contextPath}/main/assets/js/jquery.js"></script>
<script type="text/javascript" src="${contextPath}/main/assets/js/pushlet/ajax-pushlet-client.js"></script>
<script type="text/javascript">
// PL._init();
PL.joinListen('/fxh/helloworld'); //事件标识 在数据源中引用
function onData(event) {
console.log(event.get("message"));
}
</script>
</head>
<body>
</body>
</html>
本文介绍了一个使用Java实现的简单实时消息推送系统示例。该系统通过Pushlet库实现服务器到客户端的消息推送功能,包括消息推送的时间间隔设置、事件创建及发送流程。前端采用HTML与JavaScript监听并显示推送信息。
1万+

被折叠的 条评论
为什么被折叠?



