Android 聊天应用程序用户如何再次联机获取脱机时发送给他们的消息

Android 聊天应用程序用户如何再次联机获取脱机时发送给他们的消息,android,node.js,redis,socket.io,android-room,Android,Node.js,Redis,Socket.io,Android Room,我正在开发一个android聊天应用程序,使用NodeJS和redis来储存消息和用户信息。我使用socket io进行通信,并在本地数据库中存储消息。当用户脱机时,我希望他们再次联机接收消息。我的问题是,当用户A脱机时,用户B向他发送许多消息(例如5条消息),当用户A再次联机时,他只收到第一条消息,最后一条消息4次。这就是我正在做的,一旦用户收到一条消息,我就会将Redis中的消息状态从“已发送”更新为“已发送”。在用户离线的情况下,我将他们的消息以消息“已发送”的状态储存在Redis中,再次

我正在开发一个android聊天应用程序,使用NodeJS和redis来储存消息和用户信息。我使用socket io进行通信,并在本地数据库中存储消息。当用户脱机时,我希望他们再次联机接收消息。我的问题是,当用户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数据库中,然后将它们显示给用户,如下面的代码所示

下面是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();



    }
}

我通过切换到
RxJava
而不是
AsyncTask
解决了这个问题。该问题与AsyncTask有关,因为它有时会影响数据链,而
RxJava
的情况并非如此,如中所述:“AsyncTasks的另一个问题是,如果同时运行多个任务。您无法保证它们将以什么顺序完成,这导致在所有任务完成时需要检查复杂的逻辑。更糟糕的是,假设一个将在另一个之前完成,直到遇到边缘情况,使第一个调用变慢,从而使它们以错误的顺序完成,并产生不希望的结果。”

 //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();
                        }


                    }
                });
            }

        }
    });