From 2b9c93ac6d216b67e95ccc0cfb30bc5f58c9744a Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 24 Jul 2014 22:20:33 +0700 Subject: Websocket replies --- web/scripts3.js | 138 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 110 insertions(+), 28 deletions(-) (limited to 'web/scripts3.js') diff --git a/web/scripts3.js b/web/scripts3.js index 9cb72a98..3d35b441 100644 --- a/web/scripts3.js +++ b/web/scripts3.js @@ -1,43 +1,123 @@ -/* var ws=null; +var pageTitle; function initWS() { - ws = new WebSocket("wss://ws.juick.com/?hash="+hash); - ws.onopen = function() { - $('#sidepanel').addClass('online'); - }; - ws.onclose = function() { - $('#sidepanel').removeClass('online'); - ws=null; - initWS(); - }; - ws.onmessage = function(msg) { - if(msg.data==' ') { - ws.send(' '); + if(typeof(pageMID)!="undefined" && pageMID>0) { + var url; + if(typeof(juickDebug)!="undefined") { + url="wss://ws.juick.com/_replies"; } else { - try { - var jsonMsg=$.parseJSON(msg.data); - if(jsonMsg.rid>0) { - //incomingComment(jsonMsg); - } else if(jsonMsg.mid>0) { - //incomingPost(jsonMsg); - } else { - //incomingPM(jsonMsg); + url="wss://ws.juick.com/"+pageMID; + } + if(typeof(hash)!="undefined" && hash) { + url+="?hash="+hash; + } + + ws = new WebSocket(url); + ws.onopen = function() { + console.log('online'); + if($('#wsthread').length==0) { + var d=$('
'); + d.on('click',onclickNextReply); + d.appendTo("body"); + pageTitle=document.title; + } + }; + ws.onclose = function() { + console.log('offline'); + ws=null; + setTimeout(function() { + initWS(); + },2000); + }; + ws.onmessage = function(msg) { + if(msg.data==' ') { + ws.send(' '); + } else { + try { + var jsonMsg=$.parseJSON(msg.data); + console.log('data: '+msg.data); + wsIncomingReply(jsonMsg); + } catch(err) { + console.log(err); } - } catch(err) { - console.log(err); } - } - }; - setInterval(wsSendKeepAlive, 90000); + }; + setInterval(wsSendKeepAlive, 90000); + } } function wsSendKeepAlive() { - if(ws!=null) { + if(ws) { ws.send(' '); } } - */ + +function wsShutdown() { + if(ws) { + ws.onclose=function(){}; + ws.close(); + } +} + +function wsIncomingReply(msg) { + var p; + if(msg.replyto>0) { + p=$('#'+msg.replyto); + if(p.length==0) { + p=null; + } + } + + var li=$('
  • '); + li.html('
    '+ + '
    '+ + '
    '+ + ''+ + ''+ + '
    '+msg.body+'
    '+ + ''+ + ''+ + '
    '); + + if(p) { + li.css('margin-left',parseInt(p.css('margin-left'))+20+'px'); + p.after(li); + } else { + $('#replies').append(li); + } + + updateRepliesCounter(); +} + +function onclickNewReply(e) { + var li=$(e); + li.removeClass('reply-new'); + li.off('click'); + li.off('mouseover'); + updateRepliesCounter(); +} + +function onclickNextReply() { + var li=$('#replies>li.reply-new:first'); + if(li.length) { + li.removeClass('reply-new'); + li.off('click'); + li.get(0).scrollIntoView(); + updateRepliesCounter(); + } +} + +function updateRepliesCounter() { + var replies=$('#replies>li.reply-new').length; + if(replies>0) { + $('#wsthread').text(replies).css('display','block'); + document.title='['+replies+'] '+pageTitle; + } else { + $('#wsthread').css('display','none'); + document.title=pageTitle; + } +} /******************************************************************************/ /******************************************************************************/ @@ -592,4 +672,6 @@ $(document).ready(function() { unfoldReply(); $(window).bind('hashchange',unfoldPostForm); $(window).bind('hashchange',unfoldReply); + + $(window).on('beforeunload',wsShutdown); }); -- cgit v1.2.3