v1.18.x
Baris Usakli 12 years ago
parent 1c27cbd90a
commit 6a08fedf18

@ -160,15 +160,11 @@
socket.on('chatMessage', function(data) {
var username = data.username;
var fromuid = data.fromuid;
var message = data.message;
var timestamp = data.timestamp;
require(['chat'], function(chat) {
var chatModal = chat.createModalIfDoesntExist(username, fromuid, function(created, modal) {
var chatModal = chat.createModalIfDoesntExist(data.username, data.fromuid, function(created, modal) {
if(!created)
chat.appendChatMessage(modal, message, timestamp);
chat.appendChatMessage(modal, data.message, data.timestamp);
});
chatModal.show();
@ -176,6 +172,14 @@
});
});
socket.on('chatGoOffline', function(data) {
require(['chat'], function(chat) {
if(chat.modalOpen(data.uid)) {
var modal = chat.getModal(data.uid);
chat.appendChatMessage(modal, data.username + ' went offline\n', data.timestamp);
}
});
})
require(['mobileMenu'], function(mobileMenu) {
mobileMenu.init();

@ -13,12 +13,19 @@ define(['taskbar'], function(taskbar) {
chatModal.css('zIndex', topZ + 1);
}
module.getModal = function(touid) {
return $('#chat-modal-' + touid);
}
module.modalOpen = function(touid) {
return $('#chat-modal-' + touid).length !== 0;
}
module.createModalIfDoesntExist = function(username, touid, callback) {
var chatModal = $('#chat-modal-'+touid);
var chatModal = $('#chat-modal-' + touid);
if(!chatModal.length) {
var chatModal = $('#chat-modal').clone();
chatModal.attr('id','chat-modal-'+touid);
chatModal.attr('id','chat-modal-' + touid);
var uuid = utils.generateUUID();
chatModal.attr('UUID', uuid);
chatModal.appendTo($('body'));

@ -32,7 +32,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
(function(io) {
var users = {},
userSockets = {},
rooms = {}
rooms = {},
chats = {};
global.io = io;
@ -55,7 +56,14 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
io.sockets.in('global').emit('api:user.isOnline', isUserOnline(uid));
user.getUserField(uid, 'username', function(err, username) {
socket.emit('event:connect', {status: 1, username:username});
socket.emit('event:connect', {status: 1, username:username, uid:uid});
if(chats[uid]) {
for(var i=0; i<chats[uid].length; ++i) {
io.sockets.in(chats[uid][i]).emit('chatMessage', {fromuid:uid, username:username, message: username+' came online\n', timestamp: Date.now()});
socket.join(chats[uid][i]);
}
}
});
}
});
@ -74,6 +82,18 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
delete users[sessionID];
if(uid) {
io.sockets.in('global').emit('api:user.isOnline', isUserOnline(uid));
user.getUserField(uid, 'username', function(err, username) {
if(chats[uid] && chats[uid].length) {
for(var i=0; i<chats[uid].length; ++i) {
io.sockets.in(chats[uid][i]).emit('chatGoOffline', {uid:uid, username:username, timestamp:Date.now()});
socket.leave(chats[uid][i]);
}
}
});
}
}
@ -526,10 +546,8 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
var msg = utils.strip_tags(data.message);
var uids = [touid,uid].sort();
var chatroom = 'chat_' + uids[0] + '_' + uids[1];
console.log('entering chat room ', chatroom);
socket.join('chat_' + uids[0] + '_' + uids[1]);
var uids = [uid, touid].sort();
var chatroom = 'chatroom_'+uids[0]+'_'+uids[1];
user.getUserField(uid, 'username', function(err, username) {
var finalMessage = username + ': ' + msg,
@ -550,16 +568,27 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
numSockets = userSockets[touid].length;
for(var x=0; x<numSockets; ++x) {
userSockets[touid][x].join(chatroom);
userSockets[touid][x].emit('chatMessage', {fromuid:uid, username:username, message: finalMessage, timestamp: Date.now()});
}
chats[touid] = chats[touid] || [];
if(chats[touid].indexOf(chatroom) === -1)
chats[touid].push(chatroom);
}
if(userSockets[uid]) {
numSockets = userSockets[uid].length;
for(var x=0; x<numSockets; ++x) {
userSockets[uid][x].join(chatroom);
userSockets[uid][x].emit('chatMessage', {fromuid:touid, username:username, message:'You : ' + msg, timestamp: Date.now()});
}
chats[uid] = chats[uid] || [];
if(chats[uid].indexOf(chatroom) === -1)
chats[uid].push(chatroom);
}
});
});

Loading…
Cancel
Save