简体   繁体   English

客户端重新连接到服务器后,从服务器向客户端发送Java消息

[英]Java-messages to client from server after client reconnects to server

I would like to know if there is any way to get messages from one client to another client and if receiving client is not connected to a server, but connects after some time, that client should receive those messages. 我想知道是否有任何方法可以将消息从一个客户端发送到另一个客户端,并且如果接收客户端没有连接到服务器,但是在一段时间后连接,则该客户端应该接收那些消息。

Suppose client A and Client B: 假设客户端A和客户端B:

  1. Client A sends messages to client B, but client B is offline 客户端A向客户端B发送消息,但客户端B处于脱机状态
  2. Client A message must be stored on server in some sort(that I'd like to know) 客户端消息必须以某种形式(我想知道)存储在服务器上
  3. When Client B connects to a server should receive all the messages for it which were sent to this client when it was offline. 当客户端B连接到服务器时,它应接收脱机时发送给该客户端的所有消息。

Everything should be done in Java using sockets. 一切都应使用套接字在Java中完成。

Is there any mechanism to store those offline messages and then send them to a client as soon as it connects to a server? 是否有任何机制可以存储这些脱机消息,然后在它们连接到服务器后立即将其发送到客户端?

You can use different types of storage - database, in memory, some cloud storage, plain text files or whatever. 您可以使用不同类型的存储-数据库,内存,一些云存储,纯文本文件或其他。 So for example if you store all messages in memory (to make it simpler) and you have an incrementing ID for each message and a sender and receiver (I guess you can do it in different way but I am just giving a suggestion). 因此,例如,如果您将所有消息都存储在内存中(以使其更简单),并且每个消息都有一个递增的ID,并且发送方和接收方都有(我想您可以用不同的方式来做,但我只是给出一个建议)。

So when a new client connect to the server and authenticate (to make sure if Client B joins the chat it is the same Client B as before) the client can "tell" the server the last message ID received. 因此,当新的客户端连接到服务器并进行身份验证(以确保客户端B是否加入聊天之前,它与以前的客户端B相同)时,客户端可以“告诉”服务器最后收到的消息ID。 And then the server can push the missed message to the client. 然后服务器可以将丢失的消息推送到客户端。 Of course it depends a lot on your model and what exactly do you want to achieve. 当然,这很大程度上取决于您的模型以及您要实现的目标。 Probably in memory storage for message won't be the best you can use a database or something else - depending on your needs. 可能在消息的内存存储中可能不是您可以使用数据库或其他功能的最佳方式-取决于您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM