yy339452689
善良勤劳勇敢而又聪明的老杨
2017-11-21 06:52
采纳率: 0%
浏览 785

Comet4j推送页面console报错

最近在做一个页面信息推送的需求。其实很简单,就是前台页面在不刷新的情况下,会定时得到后台推送的数据来更新页面,我使用的是comet4j。但是在使用过程中遇到一些问题,百度很多,也没有相关资料。所以发帖求助。
问题:如图,在每次推送的时候页面console就报这个错误
图片说明
说明下,我jdk用的1.7,server.xml配置也已更改。有的时候可以推送成功,有的时候又会包这个错。在本地还好,基本都能成功,尤其是在linux上,就出错频率高。下面贴出我们推送的部分关键代码:
后台java代码如下:

@Override
    public boolean handleEvent(ConnectEvent event) {
        // TODO Auto-generated method stub
        final CometConnection conn = event.getConn();
        HttpServletRequest request = conn.getRequest();
        HttpServletResponse response = conn.getResponse();

        //获取当前用户
        User activeUser = ActiveUser.getInstance().getLoginUserOfSession(request, response);
        int userid = activeUser.getUserid();

        //将每一次事件时的用户id和事件存入缓存
        CacheManager.putContent(event, userid, EXPIRED_AFTER_ONE_HOUR);
        return true;
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        // CometContext : Comet4J上下文,负责初始化配置、引擎对象、连接器对象、消息缓存等。  
        CometContext cc = CometContext.getInstance();  

        // 注册频道,即标识哪些字段可用当成频道,用来作为向前台传送数据的“通道”  
        cc.registChannel(CHANNEL1);
        cc.registChannel(CHANNEL2);
        cc.registChannel(CHANNEL3);
        cc.registChannel(CHANNEL4);
        cc.registChannel(CHANNEL5);
        cc.registChannel(CHANNEL6);
        cc.registChannel(CHANNEL7);
        cc.registChannel(CHANNEL8);
        cc.registChannel(CHANNEL9);
        cc.registChannel(CHANNEL10);
        cc.registChannel(CHANNEL11);

        //添加监听器  
        CometEngine engine = CometContext.getInstance().getEngine();  
        engine.addConnectListener(this);

        //初始化时开启推送
        pushThread = new Thread(new SendToClientThread());
        pushThread.start();
    }

    //推送到指定用户和指定频道
                                   engine.sendTo(CHANNEL1,engine.getConnection(preconnid),JSON.toJSONString(personMsgList));
                                   engine.sendTo(CHANNEL2,engine.getConnection(preconnid),JSON.toJSONString(personInboxList));
                                   engine.sendTo(CHANNEL3,engine.getConnection(preconnid), JSON.toJSONString(personOutboxList));
                                   engine.sendTo(CHANNEL4,engine.getConnection(preconnid), JSON.toJSONString(groupMsgList));
                                   engine.sendTo(CHANNEL5,engine.getConnection(preconnid), JSON.toJSONString(groupInboxList));
                                   engine.sendTo(CHANNEL6,engine.getConnection(preconnid), JSON.toJSONString(groupOutboxList));
                                   engine.sendTo(CHANNEL7,engine.getConnection(preconnid), JSON.toJSONString(directMsgList));
                                   engine.sendTo(CHANNEL8,engine.getConnection(preconnid), JSON.toJSONString(directGroupMsgList));
                                   engine.sendTo(CHANNEL9,engine.getConnection(preconnid), JSON.toJSONString(directOutboxList));
                                   engine.sendTo(CHANNEL10,engine.getConnection(preconnid), JSON.toJSONString(dataMap));
                                   engine.sendTo(CHANNEL11,engine.getConnection(preconnid), JSON.toJSONString(groupParMap));

页面接收如下:
JS.Engine.on({

        res3 : function(data)
            {
                if(data != null)
                {   
                    //console.log("PersonMsgOutbox页面解析前的数据:"+data);
                    var res = eval('('+data+')');
                    //console.log("PersonMsgOutbox页面解析后的数据:"+res);
                    var str = "",msgno = "",username = "",status = "",msgtext = "",stime = "",atime = "",ackby = "",btn1 = "" ,btn2= "";

                    for ( var i = 0 ; i < res.length ; i++)
                    {
                        switch (res[i].status) {
                                    case 0:
                                            status = "Pending";
                                            break;

                                    case 1:
                                            status = "Sending";
                                            break;

                                    case 2:
                                            status = "PartialComplete";
                                            break;

                                    case 3:
                                            status = "Complete";
                                            break;

                                    case 4:
                                            status = "Fail";
                                            break;

                                    case 5:
                                            status = "Aborted";
                                            break;

                                    case 6:
                                            status = "NoDevice";
                                            break;

                                    case 7:
                                            status = "NoProfile";
                                            break;

                                    case 8:
                                            status = "NoPattern";
                                            break;

                                    case 9:
                                            status = "NoSequence";
                                            break;

                                    case 10:
                                            status = "AccountDisable";
                                            break;

                                    case 11:
                                            status = "Schedule";
                                            break;
                            }

                            if(res[i].isresend == 0)
                            {
                                btn1 = "<button class='btn btn-info' type='button'  style='margin-left:4px;' onclick='ReplyPersonMsg(\""+res[i].mrid+"\")'>Ack</button>";
                            }
                            else
                            {
                                btn1 = "<button class='btn btn-info' type='button'  style='margin-left:4px;' disabled='disabled'>Ack</button>";
                            }

                            if(res[i].status != 5)
                            {
                                btn2 = "<button class='btn btn-info' type='button' style='margin-left:4px;' onclick='editMsg(\""+res[i].msgid+"\")'>Edit</button>"+
                                       "<button class='btn btn-danger' type='button' style='margin-left:4px;' onclick='stopMsg(\""+res[i].mrid+"\")'>Abort</button>";
                            }
                            else
                            {
                                btn2 = "<button class='btn btn-info' type='button' style='margin-left:4px;' disabled='disabled'>Edit</button>"+
                                       "<button class='btn btn-danger' type='button' style='margin-left:4px;' disabled='disabled'>Abort</button>";
                            }

                            msgno = res[i].msgno == null ? "" : res[i].msgno;
                            username = res[i].username == null ? "" : res[i].username;
                            msgtext = res[i].msgtext == null ? "" : res[i].msgtext;
                            //dateFtt("yyyy-MM-dd hh:mm:ss",new Date(res[i].scheduletime))
                            stime = res[i].scheduletime == null ? "" : res[i].scheduletime;
                            atime = res[i].acktime == null ? "" : dateFtt("yyyy-MM-dd hh:mm:ss",new Date(res[i].acktime));
                            ackby = res[i].ackby == null ? "" : res[i].ackby;

                            str += "<tr>"+
                                    "<td>"+msgno+"</td>"+
                                    "<td>"+username+"</td>"+
                                    "<td>"+status+"</td>"+
                                    "<td>"+msgtext+"</td>"+
                                    "<td>"+stime+"</td>"+
                                    "<td>"+atime+"</td>"+
                                    "<td>"+ackby+"</td>"+
                                    "<td><button class='btn btn-info' type='button' id='updateDept' onclick='WatchMsgDetail(\""+res[i].mrid+"\",\""+res[i].userid+"\")'>Detail</button>"+btn1+btn2+"</td>"+
                                    "</tr>";
                    }

                        $("#outboxBody").html(str);
                }                           
            }
    });

    JS.Engine.start('conn');

请有经验的大佬指点迷津。。。如有什么需要补充,尽情提出!!谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • sjynt131
    sjynt131 2018-04-28 08:50

    调试错误不是已经报JSON转换异常吗?估计是消息中有特殊字符导致JSON解析错误,在封装消息的JSON时可以过滤特殊字符,或者调试输出下返回的JSON看看是哪里有问题

    点赞 评论

相关推荐