var lookCount = { num: 0, time: 0 }, ajaxlook = 0, lastid = '', ismore = false, loaded = true, page = 1, scrollHeight = 0, socket = null, islink = 1, ping_timer = null; //浏览器内部通过storage通讯 window.addEventListener("storage", function(e) { // 其他页面退出登录本页面需要关闭socket if (e.key == 'socketState' && e.newValue == '2') { if(socket && socket.readyState == 1){ socket.close(); } } }); $(function() { moreChat(); nowCount(); if ("WebSocket" in window) { webSocket(socketUrl); if(mine.usertype){ chatOnLoad(); }else{ chatUnlogin(); } hiddenCommon(); } else { console.log("您的浏览器不支持聊天!"); } // 发送消息 $('#send').click(function() { if(socket && socket.readyState == 1){ send(); }else{ webSocket(socketUrl); var timer = setInterval(function(){ if(socket && socket.readyState == 1){ clearInterval(timer); send(); } },500); setTimeout(function(){ if(timer){ clearInterval(timer); } },10000); } }); $("body").click(function(e){ var con = $("#commonly"); // 设置目标区域 var face = $("#face"); if ((!con.is(e.target) && con.has(e.target).length === 0) && (!face.is(e.target) && face.has(e.target).length === 0)) { commonHide(); } }); $("#face").click(function(){ face(); }); $("#chat_content").scroll(function(){ var cs = document.getElementById('chat_content').scrollTop; if(cs == 0 && ismore && loaded){ document.getElementById('chat_content').scrollTop = 21; loaded = false; showLoading(); moreChat(); } }) }); function webSocket(socketUrl){ if(islink == 1){ if (ping_timer) { clearInterval(ping_timer); ping_timer = null; } socket = new WebSocket(socketUrl); socket.onopen = function() { islink = 1; var ping = { type: 'ping' }; ping_timer = setInterval(function() { if (socket && socket.readyState == 1){ socket.send(JSON.stringify(ping)); }else{ socket.close(); } }, 50000); }; socket.onerror = function() { islink = 2; socket = null; if(ping_timer){ clearInterval(ping_timer); ping_timer = null; } }; socket.onclose = function() { socket = null; if(ping_timer){ clearInterval(ping_timer); ping_timer = null; } }; //捕捉socket端发来的事件 socket.onmessage = function(event) { var e = JSON.parse(event.data); //console.log(e); switch (e.type) { //显示收到的消息 case 'xjhMessage': getMessage(e.data); break; case 'error': islink = 2; if(ping_timer){ clearInterval(ping_timer); ping_timer = null; } break; case 'startLive': startLive(e.data); break; case 'stopLive': stopLive(e.data); break; case 'xjhCount': xjhCount(e.data); break; } }; } } // 加载聊天数据 function chatOnLoad(){ $.post(wapurl + "index.php?c=chat&a=goChat", { chattype: 'xjh' }, function(data) { if (data) { mine = data.mine; if (socket && socket.readyState == 1){ bindUser(data) }else{ var t = setInterval(function(){ if (socket && socket.readyState == 1){ clearInterval(t); t = null; bindUser(data); } },100); setTimeout(function(){ if(t){ clearInterval(t); t = null; } },10000); } } },'json'); } // 重新连接请求数据 function chatOnShow(){ $.post(wapurl + "index.php?c=chat&a=getToken", { chattype: 'xjh' }, function(data) { if (data) { if (socket && socket.readyState == 1){ bindUser(res); }else{ var t = setInterval(function(){ if (socket && socket.readyState == 1){ clearInterval(t); t = null; bindUser(res); } },500); setTimeout(function(){ if(t){ clearInterval(t); t = null; } },10000); } } },'json') } function bindUser(res){ if(res.yuntoken && res.yuntoken.token && typeof(xjhsyncid) != "undefined" && xjhsyncid != ""){ var yuntoken = res.yuntoken.token; var bind = { type: 'bind', data: { uid: mine.id, usertype: mine.usertype, yuntoken: yuntoken, group: 'xjh_' + xjhsyncid } } socket.send(JSON.stringify(bind)); window.localStorage.setItem("socketState", "1"); }else{ socket = null; islink = 2; if (ping_timer) { clearInterval(ping_timer); ping_timer = null; } } } // 未登录用户加载聊天数据 function chatUnlogin(){ $.post(wapurl + "index.php?c=chat&a=getUnloginToken", { chattype: 'xjh' }, function(res) { if (res) { var t = setInterval(function(){ if (socket && socket.readyState == 1){ clearInterval(t); // 加入宣讲会 if(res.data){ var bindData = res.data; bindData.group = 'xjh_' + xjhsyncid; var bind = { type: 'bind', data: bindData } socket.send(JSON.stringify(bind)); } window.localStorage.setItem("islink", "1"); } },100); setTimeout(function(){ if(t){ clearInterval(t); } },10000); } },'json'); } function hiddenCommon(){ // 检查页面是否后台运行 var yunhdn, yunvsb; if (typeof document.hidden !== "undefined") { yunhdn = "hidden"; yunvsb = "visibilitychange"; } else if (typeof document.msHidden !== "undefined") { yunhdn = "msHidden"; yunvsb = "msvisibilitychange"; } else if (typeof document.webkitHidden !== "undefined") { yunhdn = "webkitHidden"; yunvsb = "webkitvisibilitychange"; } document.addEventListener(yunvsb, function(){ // 前台运行后,如socket断开,重新连接 if(!document[yunhdn]){ if (!socket || (socket && socket.readyState != 1)) { webSocket(socketUrl); if(mine.usertype){ chatOnShow(); }else{ chatUnlogin(); } } } }); } function back(){ history.back(); } // 发送消息 function send() { var content = document.getElementById('send_content'); if (content.value != '') { // 发送聊天 sendMessage(content.value,'char',''); content.value = ''; } } function getMessage(msg){ if(msg.xjhid == 'xjh_' + xjhsyncid && (msg.id != mine.id || (msg.id == mine.id && msg.plat != 'wap'))) { render(msg, 'get'); } } // 发送消息 function sendMessage(content, type, id){ if(socket && socket.readyState == 1){ // 根据消息类型区分保存内容 var mineData = { id: mine.id, ftype: mine.usertype, avatar: mine.avatar, username: mine.username, timestamp: new Date().getTime(), xjhid: 'xjh_' + xjhsyncid, xid: xjhid, msgtype: type, caster: 0, plat: 'wap' }; var checkdata = { id: mine.id, content: content, xjhid: xjhid, timestamp: mineData.timestamp, }; $.post(wapurl+"/index.php?c=chat&a=checkdata", checkdata, function (res) { if(res.errcode){ showToast(res.msg) }else{ if(res.msgcontent != ''){ mineData.content = res.msgcontent; var message = { type: 'xjhMessage', data: { mine: mineData } }; socket.send(JSON.stringify(message)); // 渲染消息内容 mineData.htmlcontent = res.msgcontent; var html = render(mineData, 'send'); }else{ showToast('请慎重发言') } } },'json'); }else{ showToast(chat_name+'功能加载中') } } // 渲染消息内容 function render(msg, type){ if(type == 'send'){ // 发送 if(msg.msgtype == 'char'){ var content = rexContent(msg.htmlcontent); // 我说 var html = '