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 += ''+message.data.user+'';
html += ' at '+date_str+' '; html += ' ';
html += message.data.chat;
html += ' ';
html += ' | ';
html += '';
if (message.data.userid == '') {
html += ' ';
}
html += ' | ';
html += '
| ';
html += ' | ';
html += ' ';
html += ' ';
html += ''+date_str+' ';
// html += '';
html += ''+message.data.chat+' ';
html += ' | ';
html += '