0 0

DWR实现即时推送功能遇到的2个问题0

近日在使用DWR框架做一个网页即时推送消息的功能,遇到了两个问题
(1)推送后只有第一次是有用的,必须刷新一次页面之后才可继续用,然后继续刷新。。。(网上查了一些信息,说是用map管理scriptSession,避免新生成多余scriptiSession)
(2)我在推送信息时传了两个角色的ID,但是消息显示的时候不仅推送给那两个角色,我当前登录角色也会马上收到推送信息。
  以上两点请帮忙想一下如何解决
下面是我的部分代码:
首先是java代码:
public class PublishMessage {

    private UserService userService = AppCtxUtil.getBean(UserService.class);
    private PrescriptionConfirmService  prescriptionConfirmService = AppCtxUtil
            .getBean(PrescriptionConfirmService.class);
// 总部药师向门店推送
    public String publishMessageToStore(String message, String recordId) {

        String applicant = "";
        try {
            // 取到所有角色为药师的ID
            Conds conds = new Conds();
            conds.equal("S_ID", recordId);

            PrescriptionConfirm prescriptionConfirm = prescriptionConfirmService.fetchById(recordId);

            if (prescriptionConfirm != null) {
                applicant = prescriptionConfirm.getUserName() + ",";
                String userOrgan = userService.fetchById(applicant.substring(0, applicant.length() - 1)).getOrgan();
                Conds uConds = new Conds();
                uConds.equal("U.S_ORGAN", userOrgan);
                uConds.equal("r.s_rolename", "PHARM_STORE");
                List<User> userList = userService.fetchUserRoleByPage(uConds, null, 0, 0);
                if (userList != null) {
                    int userSum = userList.size();
                    if (userSum == 1) {
                        User user = userList.get(0);
                        applicant += user.getId() + ",";
                    }
                }
                applicant = applicant.substring(0, applicant.length() - 1);
            }
            // 推送网页脚本
            WebContext wctx = WebContextFactory.get();
            ScriptBuffer script = new ScriptBuffer();
            script.appendScript("receiveMessagesFromHQ(" + "\"" + message + "\"," + "\"" + applicant + "\"" + ");");
            ServerContext sctx = ServerContextFactory.get(wctx.getServletContext());
            String contextPath = wctx.getHttpServletRequest().getContextPath();
            Collection<ScriptSession> pages = sctx.getAllScriptSessions();
            for (ScriptSession session : pages) {
                session.addScript(script);
            }
        } catch (Exception e) {
            return "fail";
        }
        return applicant;
    }
}
其次是HTML代码(HTML代码有点多,就贴了部分哈):
$.ajax({
                                        url : "/passPrescription" + "?time=" + $.now(),
                                        type : "put",
                                        data : {
                                            id : id,
                                            remark : remark
                                        },
                                        success : function(obj) {
                                            if (null != obj) {
                                                if (obj.result == 'OK') {
                                                    alert("审核通过");
                                                    $(".main_div").load(
                                                            "/prescriptionConfirmList/1" + "/" + "id" + "?time="
                                                                    + $.now());
                                                  //发布消息
                                                    var message = "处方申请:"+id+"已通过审核,请及时复核!";
                                                    var recordId = id;
                                                    messageAjax.publishMessageToStore(message,recordId,{callback:function(data){
                                                    //alert(data);
                                                        if(data=='fail'){
                                                          alert("消息发布失败!");
                                                        }else{
                                                            if(data==''){
                                                                alert("通知信息发送失败!");
                                                            }else{
                                                                alert("已通知申请门店进行复核!");
                                                            }
                                                        }
                                                      }});
                                                } else {
                                                    alert(obj.body);
                                                }
                                            }
                                        }
                                    });

web.xml和dwr.xml是在网上照抄的,名字什么对应没问题,如果是这两个XML的问题我随后再附上内容
2013年12月19日 17:29

2个答案 按时间排序 按投票排序

0 0

可以参数 这个
http://petiro.iteye.com/admin/blogs/1562162
你现在用的是全推功能吧,里面有半推

2013年12月19日 22:01
0 0

何必自己写呢?有个叫pushlet的你可以用
例子程序网上有可以下

2013年12月19日 18:07

相关推荐

Global site tag (gtag.js) - Google Analytics