Pushlet 实现服务端往客服端推送消息
系统页面弹出消息框,类似QQ提示框
1. java代码
package com.test.jbpm.common; import java.io.Serializable;
import java.util.List;
import com.test.jbpm.web.action.BaseAction;
import nl.justobjects.pushlet.core.Dispatcher;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.Session;
import nl.justobjects.pushlet.core.SessionManager;
public class NoticePushlet extends BaseAction implements Serializable{ // 指定用户id
public void myUnicast(String user,String message) {
//必须注册ID PL.userid="hao";
if (SessionManager.getInstance().hasSession(user)) {
SessionManager.getInstance().getSessions();
Event event = Event.createDataEvent("/flowctrl/main");
event.setField("noticeKey", message);//内容不能使用中文
Dispatcher.getInstance().unicast(event, user);
} else {
//Event event = Event.createDataEvent("/jbpm_first/hao");
//event.setField("noticeKey", "The user:("+user+") not live!");
//Dispatcher.getInstance().unicast(event, this.getSession().getAttribute("code").toString());
//sDispatcher.getInstance().broadcast(event);
// Session[] sessions = SessionManager.getInstance().getSessions();
// for (Session session : sessions) {
// }
}
}
public void pushletInterface(List<String> prplflNoticeList) { Session[] sessions = SessionManager.getInstance().getSessions();
if (sessions != null && sessions.length > 0) {
for (Session session : sessions) {
String sessionId = session.getId();
String comcode = sessionId.split("->")[0];
int listLength = 0;
// for (PrplflNotice pn : prplflNoticeList) {
// if (comcode.equals(pn.getComcode())) {
// listLength = listLength + 1;
// }
// } if (listLength > 0) {
String msg = "��" + comcode + "���� " + listLength + " ��4S���뼰ʱ���������Ϣ";
Event event = Event.createDataEvent("/flowctrl/main");
event.setField("noticeKey", msg);
Dispatcher.getInstance().unicast(event, sessionId);
}
}
}
} public void myBroadcast() {
Event event = Event.createDataEvent("/flowctrl/main"); �
event.setField("noticeKey", "Broadcast msg");
Dispatcher.getInstance().broadcast(event);
}
}
2.jsp代码
需要引入ajax-pushlet-client.js
<script type="text/javascript">
// 消息提醒用js,注册监听
var usercode = '<%=session.getAttribute("code")%>';
var timestamp=new Date().getTime();
PL.userid=usercode;
//PL.userid=usercode+"->"+timestamp;
PL.webRoot="${pageContext.request.contextPath}/";
PL._init();
PL.joinListen('/flowctrl/main');
function onData(event) {
var info=event.get("noticeKey");
openDia(info);
} function openDia(info) {
document.getElementById('messageInfo').innerHTML = info +'<BR><BR><a href="javascript:void(0);" onclick="deal()" target="page">点击处理(未处理完毕仍可点击以继续)</a>';
showDiv();
}
//消息提示使用
var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,i = 0;
function showDiv(){
try{
divTop = parseInt(document.getElementById("dialog").style.top,10);
divLeft = parseInt(document.getElementById("dialog").style.left,10);
divHeight = parseInt(document.getElementById("dialog").offsetHeight,10);
divWidth = parseInt(document.getElementById("dialog").offsetWidth,10);
docWidth = document.body.clientWidth;
docHeight = document.body.clientHeight;
document.getElementById("dialog").style.top = parseInt(document.body.scrollTop,10) + docHeight - divHeight;
document.getElementById("dialog").style.left = parseInt(document.body.scrollLeft,10) + docWidth - divWidth;
document.getElementById("dialog").style.visibility="visible";
}
catch(e){}
}
function closeDiv()
{
document.getElementById('dialog').style.visibility='hidden';
}
</script>