Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

如何js是如何实现检测消息回话的,看不太懂啊?

            // 接受服务器消息
            function getServerMsg() {
                if (!exitflag) {
                    $.ajax({
                        url : "../hf/chat/get",
                        type : "post",
                        data : {
                            sessionId : sessionId
                        },
                        dataType : "json",
                        success : function(response) {
                            // 显示接收消息
                            var success=response.success;
                            if(success==false){
                                exitflag=true;
                            }else{
                                var list = response.msg;
                                if (list != null && list.length > 0)
                                    window.messageListener(list);
                                getServerMsg();
                            }
                        },
                        error : function(XMLHttpRequest, textStatus,
                                errorThrown) {
                            if (textStatus == "timeout")
                                getServerMsg();
                        }
                    });
                }
            }

window.messageListener(list);起到什么作用啊
为什么内部又调用自己getServerMsg();

            // list 消息列表,列表按消息时间顺序排列
            window.messageListener = function(list) {
                for ( var i = 0; i < list.length; i++) {
                    var msgnode = list[i];
                    if (msgnode.body == "@endconversation") {// 坐席退出聊天
                        message.clearMessageText();
                        message.overChat();// 结束会话
                        overFlag=true;
                        var leaveFlag = sessionStorage.leaveFlag;
                        if (leaveFlag && leaveFlag == "hold") {
                            message.insertMessageText("<i class='mui-icon icon-tongzhi'></i>您已退出人工服务,若继续咨询请重新进入在线咨询");
                        }
                        startFlag = false;
                        // 更新坐席信息
                        if (typeof ajid != "undefined" && ajid != '') {
                            message.insertRate(ajid);
                        }
                    } else if (msgnode.body == "@endconversation_temp") {
                        // message.overChat();// 结束会话
                    } else if (msgnode.body == "@ring") {// 排队
                        message.clearMessageText();
                        message.insertMessageText("<i class='mui-icon icon-tongzhi'></i>正在排队,您前面还有"
                                        + msgnode.queneNum + "人等候," + "请稍后");
                        // 更新坐席信息
                        // $("#img_kefu").attr("src", "./images/yewu1.png");
                    } else if (msgnode.body == "@waitin") {// 进线
                        startFlag = true;
                    } else if (msgnode.body == "@sendtimeout") {
                        var exitFlag = sessionStorage.exitFlag;
                        if (!autoExit) {
                            autoExit = true;
                            message
                                    .insertMessageText(
                                            "<i class='mui-icon icon-tongzhi'></i>由于您长时间未发送消息,人工服务将于60秒后断开",
                                            function() {
                                                var $el = $(this);
                                                var count = 60;

                                                var timers = setInterval(
                                                        function() {
                                                            $el.html("<i class='mui-icon icon-tongzhi'></i>由于您长时间未发送消息,人工服务将于"
                                                                            + count
                                                                            + "秒后断开");
                                                            if (autoExit) {
                                                                if (count == 0) {
                                                                    autoExit = false;
                                                                    clearInterval(timers);
                                                                    $el.html("");
                                                                    count = 60;
                                                                    startFlag = false;
                                                                }
                                                            } else {// 恢复正常
                                                                $el.html("");
                                                                clearInterval(timers);
                                                                count = 60;
                                                            }
                                                            count--;
                                                        }, 1000);
                                            });
                        }
                    } else if (msgnode.body == "@rectimeout") {// @rectimeout
                        message.overChat();// 结束会话
                        overFlag=true;
                        message
                                .insertMessageText("<i class='mui-icon icon-tongzhi'></i>由于坐席繁忙,暂时无法处理您的问题,请重新打开本页面...");
                        startFlag = false;
                    } else if (msgnode.type == "@deliver") {
                        var body = common.unhtml(msgnode.body);// 去除html特殊字符
                        if (body.indexOf("http") > -1&&body.indexOf(".png") > -1 ) {
                            body = '<img src="' + body + '">';
                        }
                        body = emotion.convertEmotionNew(body);
                        if (msgnode.from != "" && ajid != msgnode.from) {
                            ajid = msgnode.from;
                            sessionStorage.ajid = ajid;
                            message.clearMessageText();
                            message.insertMessageText("<i class='mui-icon icon-tongzhi'></i>客服"
                                            + ajid + "正在为您服务");
                        }
                        message.insertLeftText(body, common
                                .getFormatDateByLong(Math.round(msgnode.time),
                                        "hh:mm:ss"));

                    }

                }
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你代码里看不到 messageListener 方法的定义呀

内部调用 getServerMsg 就轮询嘛。 一直访问服务端获取最新的消息,不过感觉这个频率好高啊。

如果时效要求不高可以考虑加个节流延时一下


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...