日期:2014-05-17  浏览次数:20694 次

DWR PUSH 如何指定用户推动
DWR 如何指定用户用户推送消息,需求是这样的,用户登录平台后,调用DWR中的方法,该方法根据当前用户请求的ID生成一个线程 连接到指定的SOCKET服务器上,如果多个用户登录会生成多个,如果SOCKET服务器端有返回信息,就把该信息推动到指定的用户浏览器中去,使用DWR PUSH 如何做,web.xml dwr.xml已经配置好了,现在已经完成如下代码
Ajax 类

public class Ajax {

public ServletContext getServletContext(){
return getWebContext().getServletContext();

}
public HttpSession getSession(){
WebContext context = WebContextFactory.get();
return context.getSession();
}
public WebContext getWebContext(){
return WebContextFactory.get();
}
}

CTIListenerAjax  类DWR 方法实现类

public class CTIListenerAjax extends Ajax{

private Log logMsg = LogFactory.getLogger(CTIListenerAjax.class);
public void ctiListener(){
String empCode =(String)this.getSession().getAttribute("EMPCODE");
ServerUserThread sut = new ServerUserThread();
sut.setSc(this.getServletContext());
sut.setEmpCode(empCode);
sut.setWc(this.getWebContext());
Thread thread = new Thread(sut);
thread.start();
}
}

ServerUserThread 用户线程类,其中CTIClient是当前用户连接socket的功能类,该类根据用户ID 连接到SOCKET服务器端,服务器端根据用户ID ,返回对应内容,改内容什么时间返回不确定

public class ServerUserThread implements Runnable {

private ServletContext sc ;
private String empCode;
private WebContext wc ;
private boolean tStatus=true;

public ServletContext getSc() {
return sc;
}

public void setSc(ServletContext sc) {
this.sc = sc;
}

public String getEmpCode() {
return empCode;
}

public void setEmpCode(String empCode) {
this.empCode = empCode;
}

public WebContext getWc() {
return wc;
}

public void setWc(WebContext wc) {
this.wc = wc;
}

public void run() {

try {

while(tStatus){
    CTIManager ctiM = new CTIManager(sc);
    CTIUserInfo ctiUser = ctiM.getCTIUserInfo(empCode);
    CTIClient ctiClient = new CTIClient();
    ctiClient.setCtiUser(ctiUser);
    String result = ctiClient.getCTIMsg(empCode);
                            如何返回信息
}

} catch (Exception e) {
e.printStackTrace();
}
}

public boolean isTStatus() {
return tStatus;
}

public void setTStatus(boolean status) {
tStatus = status;
}

}