我在聊天应用程序中使用socket io和node。有两个问题
socket.on('iAmOnline',function(data){
if(typeof usernames[data.id] != 'undefined'){
delete usernames[data.id];
delete onlineClients[data.id];
if(typeof isChatScreen[data.id] != 'undefined'){
delete isChatScreen[data.id];
}
}
socket.username = data.id;
usernames[data.id] = data.id
onlineClients[data.id] = socket.id;
if(typeof data.isChatScreen != 'undefined'){
isChatScreen[data.id] = data.isChatScreen;
}else{
isChatScreen[data.id] = 1;
}
if(typeof usernames[data.id] != 'undefined'){
var allChats = getChats(data.id, function(chats) {
var a = chats.pendingMessage;
var b = chats.deliveredMessage;
var c = chats.readMessage;
for(var i=0;i<a.length;i++){
socket.emit("message", {'from':a[i]['userAId'],'to':a[i]['userBId'], 'msg':a[i]['message'],'key':a[i]['uniqueKey']});
}
for(var i=0;i<b.length;i++){
socket.emit("delivered", {'from':b[i]['userBId'],'to':b[i]['userAId'], 'localId':0,'key':b[i]['uniqueKey']});
}
for(var i=0;i<c.length;i++){
socket.emit("read", {'from':c[i]['userBId'],'to':c[i]['userAId'], 'localId':0,'key':c[i]['uniqueKey']});
}
});
allChats = null;
}
});
function getChats(userId, callback){
return request({
uri: "http://twango.me/api/v1/chats?userId="+userId,
method: "GET",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
callback(JSON.parse(body).response);
});
}