var chat = function() { var _username; var _lastUser; var _lastChatter_id; var _lastid; var _lastgroupchatcommentid; var _chatSubscription; var _chatSubscriptions = []; var _metaSubscriptions = []; var _handshook = false; var _connected = false; var _cometd; var _subscriptionQueue = []; var _onConnect = null; return { init: function() { _cometd = $.cometd; // _chatUnsubscribe(); _metaSubscribe(); // cometURL = 'http://devwww1.blogged.com:8080/cometd'; cometURL = 'http://chat.blogged.com:8080/cometd'; _cometd.init(cometURL); // _chatSubscribe('/chat/1234'); // _chatSubscribe('/chat/12345'); // _chatSubscription = _cometd.subscribe('/chat/demo4', this, receive); // // _username = 'x'; // text = ''; // _cometd.publish('/chat/1234', { // user: _username, // chat: text // }); // // _username = 'x5'; // text = '555'; // _cometd.publish('/chat/12345', { // user: _username, // chat: text // }); // _onConnect = ChatConnected; // $.each(_chatSubscriptions, function(index, subscription) { // console.dir(subscription); // }); }, chatSubscribe: function(channel) { // console.log("subscribing to " + channel); _chatSubscribe(channel); }, chatPublish: function(channel, data) { // console.log("publishing to " + channel); _cometd.publish(channel, data); }, chatReceive: function(message) { receive(message); } } function _chatUnsubscribe() { $.each(_chatSubscriptions, function(index, subscription) { if (subscription) _cometd.unsubscribe(subscription); }); _chatSubscriptions = []; // if (_chatSubscription) _cometd.unsubscribe(_chatSubscription); // _chatSubscription = null; // // if (_chatSubscription2) _cometd.unsubscribe(_chatSubscription2); // _chatSubscription2 = null; } function _chatSubscribe(channel) { // _chatUnsubscribe(); // check if this channel is subscribed already $.each(_chatSubscriptions, function(index, subscription) { if (subscription[0] == channel) { // console.log('already subscribed skipping: ' + channel); return; } }); if (!_connected) { _subscriptionQueue.push(channel); } else { _chatSubscriptions.push(_cometd.subscribe(channel, this, receive)); } } function _metaUnsubscribe() { $.each(_metaSubscriptions, function(index, subscription) { _cometd.removeListener(subscription); }); _metaSubscriptions = []; } function _metaSubscribe() { _metaUnsubscribe(); _metaSubscriptions.push(_cometd.addListener('/meta/handshake', this, _metaHandshake)); _metaSubscriptions.push(_cometd.addListener('/meta/connect', this, _metaConnect)); } function _metaHandshake(message) { _handshook = message.successful; _connected = false; receive({ data: { user: 'CHAT', join: true, chat: 'Handshake ' + (_handshook ? 'successful' : 'unsuccessful') } }); } function _metaConnect(message) { var wasConnected = _connected; _connected = message.successful; if (wasConnected) { if (_connected) { // Normal operation, a long poll that reconnects } else { // Disconnected receive({ data: { user: 'CHAT', join: true, chat: 'Disconnected' } }); } } else { if (_connected) { receive({ data: { user: 'CHAT', join: true, chat: 'Connected' } }); // _cometd.startBatch(); $.each(_subscriptionQueue, function(index, channel) { // console.log('Queued subscription ' + channel); _chatSubscribe(channel); }); // _cometd.endBatch(); _subscriptionQueue = []; // _onConnect.apply(this); // _chatSubscribe('/chat/1234'); // _chatSubscribe('/chat/12345'); // _cometd.publish('/chat/demo4', { // user: _username, // join: true, // chat: _username + ' has joined' // }); // _cometd.endBatch(); } else { // Could not connect receive({ data: { user: 'CHAT', join: true, chat: 'Could not connect' } }); } } } function send() { var phrase = $('#phrase'); var text = phrase.val(); phrase.val(''); if (!text || !text.length) return; var colons = text.indexOf('::'); if (colons > 0) { _cometd.publish('/service/privatechat', { room: '/chat/demo4', // This should be replaced by the room name user: _username, chat: text.substring(colons + 2), peer: text.substring(0, colons) }); } else { _cometd.publish('/chat/demo4', { user: _username, chat: text }); } } function receive(message) { // console.dir(message); if (message.data.type == "articlecomment") { if (_lastChatter_id == message.data.chatter_id) { return; } if (message.data.userid == 'blogged') return; var chat = $('#comment_list'+message.data.article_id); // console.dir(chat); var html = ''; // html += " "+message.data.user+": "+message.data.chat + "
"; var date_str = message.data.chatter_timestamp; html += '
'; html += ''; html += ''; html += ''; html += ''; html += '
'; html += ''+message.data.user+''; html += ' at '+date_str+'
'; html += '
'; html += message.data.chat; html += '
'; html += '
'; if (message.data.userid == '') { html += ''; } html += '
'; html += '
'; html += '
'; chat.append(html); var contentdiv = chat; contentdiv.scrollTop(contentdiv[0].scrollHeight - contentdiv.outerHeight()); if ($('#popup_landing_id').val() == message.data.article_id) { var chat = $('#popup_comment_list'); var html = ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += '
'; html += ''; html += ''; html += '
'; html += '
'+date_str+'
'; // html += ''; html += '
'; html += '
'+message.data.chat+'
'; html += '
'; html += '
'; chat.append(html); } _lastChatter_id = message.data.chatter_id; $('#comment_wrapper_'+message.data.article_id).css({'display':'block'}); $('#embed_link'+message.data.article_id).css({'display':''}); hideLoading2(); return; } if (message.data.type == "groupchatcomment") { if (_lastgroupchatcommentid == message.data.groupchat_comment_id) { return; } // check to see if the groupchat dialog is focused // add the blinking status to the groupchat dialog if (!$('#groupchat_textarea'+message.data.groupchat_id).hasClass('focus')) { $('#groupchat_titlebar'+message.data.groupchat_id).addClass('groupchat_unread'); } var contentdiv = $('#groupchat_content'+message.data.groupchat_id); html = ''; html += '
'; html += '
'; html += ''; html += '
'; html += '
'; html += ''+message.data.user+''; html += ''+message.data.chatter_timestamp+''; html += '
'; html += '
'; html += message.data.chat + '
'; html += '
'; contentdiv.append(html); contentdiv.scrollTop(contentdiv[0].scrollHeight - contentdiv.outerHeight()); hideLoading2(); _lastgroupchatcommentid = message.data.groupchat_comment_id; return; } if (message.data.type == "groupchatping") { var groupchat_div = document.getElementById('#groupchat_div'+message.data.groupchat_id); if (groupchat_div == null || groupchat_div.style.display == 'none') { GroupChatDialog({groupchat_id:message.data.groupchat_id, highlight_titlebar:true}); } // console.log(message.data.groupchat_id); // $('#groupchat_titlebar'+message.data.groupchat_id).addClass('groupchat_unread'); // $('#groupchat_statusbar'+message.data.groupchat_id).addClass('groupchat_unread'); // newChatAlert(); } // if (message.data instanceof Array) // { // var list = ''; // $.each(message.data, function(index, datum) // { // list += datum + '
'; // }); // $('#members').html(list); // } // else // { // var chat = $('#debug'); // // var fromUser = message.data.user; // var membership = message.data.join || message.data.leave; // var text = message.data.chat; // if (!text) return; // //// if (!membership && fromUser == _lastUser) //// { //// fromUser = '...'; //// } //// else //// { // _lastUser = fromUser; // fromUser += ':'; //// } // //// if (membership) //// { //// chat.append('' + fromUser + ' ' + text + '
'); //// _lastUser = ''; //// } //// else if (message.data.scope == 'private') //// { //// chat.append('' + fromUser + ' [private] ' + text + '
'); //// } //// else //// { //// chat.append('' + fromUser + ' ' + text + '
'); //// } // chat.append(text + "
"); // // // There seems to be no easy way in jQuery to handle the scrollTop property // chat[0].scrollTop = chat[0].scrollHeight - chat.outerHeight(); // } } }();