Skip to content

Commit

Permalink
[live-chat] 0.9.1 - Fixing issues with the ProxyReverse door
Browse files Browse the repository at this point in the history
  • Loading branch information
Nassim Ghandour committed Jan 29, 2014
1 parent 17871f7 commit dbe41a6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion client/Layout/chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
<script type="text/javascript" src="/js/libs/discussions.js?1381949847"></script>
<script type="text/javascript" src="/js/libs/moment.min.js?1381949847"></script>
<script type="text/javascript" src="/js/libs/moment-lang/fr.js?1381949847"></script>
<script type="text/javascript" src="/js/chat/app_ux-0.9.js?1381949847"></script>
<script type="text/javascript" src="/js/chat/app_ux-0.9.1.js?1381949847"></script>
<?php if ($luser['type'] == 'insider'): ?>
<script type="text/javascript" src="/js/chat/app_admin_ux.js?1381949847"></script>
<?php endif; ?>
Expand Down
28 changes: 16 additions & 12 deletions client/js/chat/app_ux-0.9.js → client/js/chat/app_ux-0.9.1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// © 2013 - PathMotion - Career Inspiration Chat - v0.9
// © 2013 - PathMotion - Career Inspiration Chat - v0.8
// app_ux.js - Manage the interactions with the node.js server

var socket = null;
Expand Down Expand Up @@ -32,16 +32,15 @@ $(document).addEvent('domready', function() {
var discussionsFilter = $('discussions_filter');
var IOconnectTimeout = null;
var ioscript = [
{
/* via ProxyReverse compatible with Firewall or Company proxy */
'server': '//'+window.location.hostname+'/socket.io/',
'lib': '//'+window.location.hostname+'/socket.io/socket.io.js'
},

{
/* direct */
'server': 'http://'+window.location.hostname+':843/',
'lib': 'http://'+window.location.hostname+':843/socket.io/socket.io.js'
},
{
/* via ProxyReverse compatible with Firewall or Company proxy */
'server': '//'+window.location.hostname+'/',
'lib': '//'+window.location.hostname+'/socket.io/socket.io.js'
}
];
var ioscript_used;
Expand All @@ -64,21 +63,25 @@ $(document).addEvent('domready', function() {
// functions

function appendScript(pathToScript) {
//console.log('appendScript: '+pathToScript);
var head = document.getElementsByTagName("head")[0];
var js = document.createElement("script");
js.type = "text/javascript";
js.src = pathToScript;
js.id = 'iojsloader';
try{
head.appendChild(js);
} catch(e){ }
}

function initIO( ioscript_previously_tested, firstAttempt ){
try{
setTimeout(function(){
if (typeof io != 'undefined'){
ioscript_used = ioscript_previously_tested;
ConnectingMsgHandler.set('html', __t('Connecting…'));
setTimeout(function(){
socket = io.connect(ioscript[0]['server'], {
socket = io.connect(ioscript[ ioscript_previously_tested ]['server'], {
// 'connect timeout': 30000,
'sync disconnect on unload': true
});
Expand All @@ -90,9 +93,10 @@ $(document).addEvent('domready', function() {
ConnectingMsgHandler.set('html', __t('Waiting for the server to be ready…'));
$('iojsloader').destroy();
appendScript( ioscript[ (firstAttempt || ioscript_previously_tested ? 0 : 1) ]['lib'] );
setTimeout(function(){ initIO( (ioscript_previously_tested ? 0 : 1), false ); }, 5000);
setTimeout(function(){ initIO( (firstAttempt || ioscript_previously_tested ? 0 : 1), false ); }, 5000);
}
}, 5000);
}catch(e){}
}

function close_chat(){
Expand Down Expand Up @@ -681,9 +685,9 @@ $(document).addEvent('domready', function() {

// socket has been closed
// setTimeout(function(){
socket.on('disconnect', function () {
return close_chat();
})
// socket.on('disconnect', function () {
// return close_chat();
// })
// }, 2000);

socket.on('connect_failed', function () { restart(); })
Expand Down
51 changes: 28 additions & 23 deletions server/chat-app-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
** PM CHAT for Career Inspiration app - v0.9
** PM CHAT for Career Inspiration app - v0.10
** nassim@pathmotion.com - 2013-2014
**
** TODOs
Expand Down Expand Up @@ -63,8 +63,8 @@ function _log(str, sessionID){
if (typeof users[ sessionID ] != "undefined" && typeof users[ sessionID ].chatDiscussionId != "undefined")
connected_user_nb = Object.keys(users_ids[ users[ sessionID ].chatDiscussionId ]).length;
console.log(moment().format('YYYY-M-D H:mm:ss')
//+(users[ sessionID ] && users[ sessionID ].socket ? "\tsocket#" + users[ sessionID ].socket.id : '')
//+ (typeof sessionID != "undefined" ? "\tsession#" + sessionID : '')
+(users[ sessionID ] && users[ sessionID ].socket ? "\tsocket#" + users[ sessionID ].socket.id : '')
+ (typeof sessionID != "undefined" ? "\tsession#" + sessionID : '')
+ "\t"+(users[ sessionID ] && users[ sessionID ].kinfOfAccess != "undefined" ? users[ sessionID ].kinfOfAccess : 'unknown_io')
+(users[ sessionID ] && users[ sessionID ].chatId ? "-chat#" + users[ sessionID ].chatId : '')
+"-nb_user("+connected_user_nb+")"
Expand Down Expand Up @@ -179,7 +179,7 @@ function removeItemsFromCache(deleted_items, sessionID){

// and then update the root discussion thread count
for (var i=0; discussionsList[ users[ sessionID ].chatDiscussionId ][i]; i++)
discussionsList[ users[ sessionID ].chatDiscussionId ].forEach(function(item_cached, index, theArray){
discussionsList[ users[ sessionID ].chatDiscussionId ].forEach(function(item_cached, index, theArray){
if (item_cached['id'] == item['parent_discussion_reply_id']){
discussionsList[ users[ sessionID ].chatDiscussionId ][index]['thread_count'] -= 1;
}
Expand Down Expand Up @@ -217,26 +217,11 @@ if (server_start){
);
}

io.set('authorization', function (handshakeData, accept) {
if (handshakeData.headers.cookie) {
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
if (handshakeData.cookie[ config.cookieName ]){
accept(null, true);
} else{
console.log('Bad cookie transmitted!')
return accept('Bad cookie transmitted.', false);
}
} else {
console.log('No cookie transmitted!')
return accept('No cookie transmitted.', false);
}
});

io.sockets.on('connection', function(socket){

// a user here will be identified as uniq throw their session identifier
var sessionID = getPmCookie( socket.handshake );
//_log('New connection identified with the session', sessionID);
_log('New connection identified with the session', sessionID);

// check if previous data linked to this sessionID
if (users[ sessionID ] && users[ sessionID ].chatId && users[ sessionID ].chatDiscussionId){
Expand All @@ -263,6 +248,8 @@ io.sockets.on('connection', function(socket){
var connected_user_nb = Object.keys(users_ids[ users[ sessionID ].chatDiscussionId ]).length;
io.sockets.emit('total online users', signEmitData( connected_user_nb ));
//_log('Number of users currently connected to the chat: '+connected_user_nb);

check_online_insiders();
}
}
/*
Expand Down Expand Up @@ -325,9 +312,26 @@ io.sockets.on('connection', function(socket){
if (users[ sessionID ].chatId && users[ sessionID ].chatDiscussionId){
checkChatDiscussion( users[ sessionID ].chatId, users[ sessionID ].chatDiscussionId );
}
check_online_insiders();
return true;
}

function check_online_insiders(){
// updating insiders list if this user was an insider
if (typeof users[ sessionID ].insider_html_identity != "undefined"
&& myCheerioDom && !myCheerioDom('#'+users[ sessionID ].user_id).html())
{
_log('Updating online insiders (+1 - reconnect)...');
myCheerioDom('ul').append( users[ sessionID ].insider_html_identity );
myCheerioDom('span.counter').html( parseInt( myCheerioDom('span.counter').html() )+1 );
io.sockets.emit('insiders-online-update', signEmitData( myCheerioDom.html() ));
// log the connection datetime
connection.query('UPDATE fbpages_discussions_chats_users '
+' SET disconnect_date = NULL '
+' WHERE discussion_chat_id = ? AND user_id = ?',
[users[ sessionID ].chatId, users[ sessionID ].user_id], function(err, info) {});
}
}

// ********** // ********** // ********** // ********** //
// ************ GENERAL REQUESTS AVAILABLE ************ //
Expand Down Expand Up @@ -459,7 +463,7 @@ io.sockets.on('connection', function(socket){

if (!is_numeric(thread_id) || thread_id == 0){
// new discussion (= first level post)
discussionsList[ users[ sessionID ].chatDiscussionId ].push(tmp);
discussionsList[ users[ sessionID ].chatDiscussionId ].splice(0,0,tmp);
io.sockets.emit('new discussion received', signEmitData(tmp));
// increment the discussions posted counter
connection.query('UPDATE fbpages_discussions_chats_users '
Expand Down Expand Up @@ -559,7 +563,7 @@ io.sockets.on('connection', function(socket){

// Update insiders list
socket.on('insiders-online', function(data){
_log('Updating online insiders...');
_log('Updating online insiders (+1)...');
myCheerioDom = cheerio.load(data);
io.sockets.emit('insiders-online-update', signEmitData( myCheerioDom.html() ));
});
Expand All @@ -578,10 +582,11 @@ io.sockets.on('connection', function(socket){
if (users[ sessionID ].user_role == 'insider'){
// insidersOnline.removeChild(insidersOnline.getElementById(users[ sessionID ].user_id));
if (myCheerioDom && myCheerioDom('#'+users[ sessionID ].user_id)) {
users[ sessionID ].insider_html_identity = myCheerioDom('#'+users[ sessionID ].user_id);
myCheerioDom('#'+users[ sessionID ].user_id).remove();
myCheerioDom('span.counter').html( parseInt( myCheerioDom('span.counter').html() )-1 );

/* temp : disable to avoid insiders becoming disconnecting again and again on the chat UI */
_log('Updating online insiders (-1)...');
io.sockets.emit('insiders-online-update', signEmitData( myCheerioDom.html() ));
}
}
Expand Down

0 comments on commit dbe41a6

Please sign in to comment.