android - 聊天应用用户如何再次在线获取离线时发送给他们的消息
问题描述
我正在开发一个 android 聊天应用程序,使用 Node Js 和 redis 来存储消息和用户信息。我正在使用 socket io 进行通信,并使用 Room 将消息存储在本地数据库中。当用户离线时,我希望他们再次在线接收他们的消息。我的问题是,当用户 A 离线时,用户 B 向他发送了许多消息(例如 5 条消息),当用户 A 再次在线时,他只收到第一条消息,最后一条消息 4 次。这是我正在做的事情,一旦用户收到消息,我将 Redis 中的消息状态从“已发送”更新为“已交付”。在用户离线的情况下,我将他们的消息以消息“已发送”的状态存储在 Redis 中,然后再次在线,我检查他们收到的消息,例如从用户 B 收到的消息,如果他们的状态是“
//On this event, we update the socket ID of the sender in Redis so they can
receive private messages from their contacts
socket.on('sender', (sender, destinat) =>{
tempId = socket.id;
senderId = sender;
users[sender] = sender;
users [destinat] = destinat;
//We also update the user status: online
client.hset(senderId, 'lastSeen', 'Now', function(reply){
console.log( senderId + reply);
});
//Stocking to the user socket id
client.hset(users[sender], 'tempId', tempId, function(){
console.log("Welcome " + sender);
console.log("Welcome " + tempId);
});
//Getting all the messages of the sender from users
//If the sender has any messages that hasn't received yet, they'll be sent
here
//the id of each message is compsed of two parts: the phone number of the
receiver, and the id of the message itself
(receiverPhoneNumber:idMessage)
client.keys(users [sender] + ':*', function(err, results) {
results.forEach(function(key) {
client.hgetall(key, function(err, reply){
if(err)
console.log(err);
else if(reply){
//Compare the message status: if not sent, deliver it to receiver once online
if('Sent'.localeCompare(reply.status) == 0 && users
[destinat].localeCompare(reply.fromUser) == 0) {
io.to(tempId).emit('message', reply);
}
}
});
});
});
});
我从服务器接收到消息后,使用 Async 将它们存储在 Room Database 中,然后显示给用户,如下代码所示
这是 AsyncTask 类:
class AddMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
//Creating a user account
m = new Message();
m.setContent( message );
m.setTime( time );
m.setUrl( url );
m.setStatus( status );
m.setFromUser( fromUser );
m.setToUser( toUser );
m.setUsername( receiver.getUsername() );
//adding to database
DatabaseClient.getInstance(getContext()).getAppDatabase()
.messageDao()
.insert(m);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText( getContext(), "Added!", Toast.LENGTH_SHORT ).show();
}
}
我已经检查是否正确地从服务器接收到 android 应用程序的消息(通过将消息再次发送到服务器,一旦传递到应用程序)。我相信这个问题与AsyncTask有关,但我就是想不通,非常感谢任何帮助,非常感谢。
//When receving a message
socket.on("message", new Emitter.Listener() {
@Override
public void call(final Object... args) {
if(getActivity() != null){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
try {
//extract data from fired event
idMessage = data.getString( "idMessage" );
message = data.getString("message");
fromUser = data.getString( "fromUser" );
toUser = data.getString( "toUser" );
time = data.getString( "time" );
status = data.getString( "status" );
url = data.getString( "url" );
//Here we call asyncTask to Add it to Database
addMessage = new AddMessage();
addMessage.execute( );
//We emit this event to update the status of
the message to delivered
socket.emit( "sent", idMessage, userID );
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
});
解决方案
RxJava
我通过切换到而不是解决了这个问题AsyncTask
。该问题与 AsyncTask 有关,因为它有时会影响数据链,而 不是这种情况RxJava
,如此链接中所述:“AsyncTasks 的另一个问题是,如果您同时运行多个。您无法保证他们将以什么顺序完成, 导致检查所有任务何时完成的复杂逻辑. 更糟糕的是假设一个将在另一个之前完成, 直到你遇到一个边缘情况, 使第一次调用变慢, 这使得它们以错误的顺序完成和不希望的结果。”