$().ready(function(){ var index = 0; var debug = false; var BOSH_SERVICE = 'http-bind.php'; var fromId = $("#from").val(); var password = $("#password").val(); var toId = $("#to").val(); var username = $("#username").val(); var customername = $("#customername").val(); var url = window.location.href; url = url.substring(0, url.indexOf("chat.php")); // if($("#error").html().length > 0){ // return; // } /** * 页面加载 */ var connection = new Strophe.Connection(BOSH_SERVICE); /** * * 函数 * */ Strophe.log = function(level, msg) { $('#log').append('
').append(document.createTextNode(msg)); }; function log(msg) { $('#log').append('').append(document.createTextNode(msg)); } /** * 播放声音 * * @param type 类型 2-客服消息 3-系统消息 */ function playSound(type){ if($("#sound").size() == 0){ $("body").append(""); } if(type == 2){ if($.browser.msie && $.browser.version == '8.0'){ //本来这里用的是"+message+"
"); $("#scroll_div").scrollTop($(".scroll_inner").height());// 发送消息后消息滚动到最底部 playSound(type); } // 发送消息 function sendMessage(){ // var message = $.trim($('#editor').val()); var message = getContent(); if(message.length>0){ var reply = $msg({ to : toId, from : fromId, type : 'chat' }).cnode(Strophe.xmlElement('body', '', message)); connection.send(reply.tree()); appendMessage(username, message, 1); } // $('#editor').val(''); //点击发送后清空代码 setContent(""); } /** * 后台发送的用户信息,报告用户页面的状态等信息 */ function sendUserStatusMessage(message){ var reply = $msg({ to : toId, from : fromId, type : 'chat' }).cnode(Strophe.xmlElement('body', '', message)); connection.send(reply.tree()); } function startConnection(){ /** * * 连接 * */ connection.rawInput = function (data) { log('RECV: ' + data); }; connection.rawOutput = function (data) { log('SENT: ' + data); }; connection.connect(fromId, password, function(status){ if (status == Strophe.Status.CONNECTING) { if(debug){ appendMessage("系统", '正在连接...', 3); } } else if (status == Strophe.Status.CONNFAIL) { if(debug){ appendMessage("系统", '连接失败!', 3); } } else if (status == Strophe.Status.AUTHENTICATING) { if(debug){ appendMessage("系统", '正在认证...', 3); } } else if (status == Strophe.Status.AUTHFAIL) { if(debug){ appendMessage("系统", '认证失败!', 3); } // 如果认证失败则重新认证 $.get('chat.php?act=authfail', function(data){ data = $.parseJSON(data); if(data != null){ if(data.error == 1){ appendMessage("系统", data.message, 3); }else{ appendMessage("系统", data.message, 3); } } }, 'text') } else if (status == Strophe.Status.DISCONNECTING) { if(debug){ appendMessage("系统", '正在断开连接...', 3); } } else if (status == Strophe.Status.DISCONNECTED) { if(debug){ appendMessage("系统", '连接已断开。', 3); } } else if (status == Strophe.Status.CONNECTED) { if(debug){ appendMessage("系统", '连接成功!', 3); } var notice = $("#system_notice").html(); if(notice.length > 0){ appendMessage("系统", notice, 3); } // 发送客户端状态 var presence = $pres().c("status").t("在线").up().c("priority").t("1"); connection.send(presence.tree()); //处理接收到的消息 connection.addHandler(function(msg) { var to = msg.getAttribute('to'); var from = msg.getAttribute('from'); var type = msg.getAttribute('type'); var elems = msg.getElementsByTagName('body'); if (type == "chat" && elems.length > 0) { if(from.indexOf("@") != -1){ from = from.substring(0, from.indexOf("@")); } var body = elems[0]; appendMessage(from, Strophe.getText(body), 2); $(document).find("title").html("在线客服聊天:您收到了一条新消息!"); } window.focus(); return true; }, null, 'message', null, null, null); // 发送用户接入信息 var message = "用户【"+username+"】接入聊天系统!"; //发送相关消息 var chat_goods_id = $("#chat_goods_id").val(); if(chat_goods_id.length > 0){ var goods_url = url + "goods.php?id="+chat_goods_id; message = message + "\n正在浏览的商品:" + goods_url; } // var chat_supp_id = $("#chat_supp_id").val(); // // if(chat_supp_id.length > 0){ // var supp_url = url + "supplier.php?suppId="+chat_supp_id; // sendUserStatusMessage("用户【"+username+"】正在浏览商品:" + supp_url); // } var chat_order_id = $("#chat_order_id").val(); if(chat_order_id.length > 0){ message = message + "\n可能需要咨询的订单ID:" + chat_order_id; } var chat_order_sn = $("#chat_order_sn").val(); if(chat_order_sn.length > 0){ message = message + "\n可能需要咨询的订单号:" + chat_order_sn; } //发送消息 sendUserStatusMessage(message); } }); } function getContent(){ return $.trim($('#editor').val()); } function setContent(msg){ $('#editor').val(msg); } /** * 编辑窗口的回车事件 */ $("#editor").keypress(function(e) { if (e.which == 13) { sendMessage(); } }); /** * 发送消息 */ $('#btn_send').click(function(){ sendMessage(); }); if(toId == null || toId.length == 0){ //alert("3.用户数据不完整,请重新刷新页面!"); var notice = $("#system_notice").html(); if(notice.length > 0){ appendMessage("系统", notice, 3); } return; } if(fromId == null || fromId.length == 0){ //alert("1.用户数据不完整,请重新刷新页面!"); return; } if(password == null || password.length == 0){ //alert("2.用户数据不完整,请重新刷新页面!"); return; } // 连接服务 startConnection(); });