使用 XMPP 服务器的聊天应用程序

发布于 2024-12-27 05:05:28 字数 706 浏览 0 评论 0原文

我正在使用 XMPP 服务器制作聊天应用程序。聊天应用程序运行良好,但我有一个问题:当两个用户之间聊天时,我无法知道来自第一个用户的消息是否已到达服务器。

那么如何知道来自第一个用户的消息是否到达服务器,而不必担心第二个用户是否在线或离线。

请帮助

我发送这样的消息:

final SecureConnection sc = (SecureConnection)Connector.open("ssl://...", Connector.READ_WRITE);
is = sc.openInputStream();
os = sc.openOutputStream();
this.reader = new XmlReader(is);
this.writer = new XmlWriter(os);

public boolean sendMessage(final String to, final String msg) {
this.writer.startTag("message"); 
this.writer.attribute("type", "chat"); 
this.writer.attribute("to", to); 
this.writer.startTag("body"); 
this.writer.text(msg); 
this.writer.endTag(); 
this.writer.endTag(); 
this.writer.flush();
}

I am making a chat application using an XMPP server. The chat application works great but I have a problem: when chatting between 2 users, I cannot know if the message from the first user has reached the server or not.

So how to know if a message from a first user reached the server without having to be worried about the second user if he is online or offline.

Please HELP

I am sending the message like this:

final SecureConnection sc = (SecureConnection)Connector.open("ssl://...", Connector.READ_WRITE);
is = sc.openInputStream();
os = sc.openOutputStream();
this.reader = new XmlReader(is);
this.writer = new XmlWriter(os);

public boolean sendMessage(final String to, final String msg) {
this.writer.startTag("message"); 
this.writer.attribute("type", "chat"); 
this.writer.attribute("to", to); 
this.writer.startTag("body"); 
this.writer.text(msg); 
this.writer.endTag(); 
this.writer.endTag(); 
this.writer.flush();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2025-01-03 05:05:28

如果您连接到服务器并发送消息,您可以相当确信该消息已到达服务器。如果客户端位于不可靠的网络上,有时 TCP 连接会悄无声息地中断,并且需要一段时间才能检测到这种情况并将其关闭。可能的解决方案(按复杂程度排序):

  • 使用 XEP-0199。
  • 实施XEP-0198。最终的可靠性,但实施起来比较棘手,并且需要服务器支持。

您似乎担心的另一个问题是 user2 看到该消息。我不知道您的应用程序、它是如何配置的或您希望它如何工作。然而,大多数 XMPP 服务器会在用户离线时自动存储发送给用户的消息。然后,当用户上线时,将它们交付给用户。有关详细信息,请参阅 XEP-0160

最后,您可以使用消息收据来了解 user2 何时收到/阅读了消息。这些在 XEP-0184 中进行了描述,并且可能是唯一 如果您只关心知道 user2 收到了消息,那么您真正需要实现的事情。

If you are connected to the server and you send a message, you can be reasonably confident the message reached the server. If the client is on an unreliable network sometimes TCP connections do silently break, and it is a while before this is detected and they are closed. Possible solutions to this (in order of complexity):

  • Ping the server at regular intervals using XEP-0199.
  • Implement XEP-0198. Ultimate reliability, but more tricky to implement and requires server support.

The other issue you seem to be worried about is user2 seeing the message. I don't know about your application, how it is configured or how you want it to work. However most XMPP servers will automatically store messages sent to a user while they are offline. They are then delivered to the user when they come online. See XEP-0160 for more information.

Finally, you can use message receipts to know when user2 has received/read the message. These are described in XEP-0184, and are possibly the only thing you really need to implement if all you care about is knowing that user2 received the message.

莫相离 2025-01-03 05:05:28

您需要在代码中实现 ACK/NACK 通知。

you need to implement ACK/NACK notification in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文