使用 XMPP 服务器的聊天应用程序
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您连接到服务器并发送消息,您可以相当确信该消息已到达服务器。如果客户端位于不可靠的网络上,有时 TCP 连接会悄无声息地中断,并且需要一段时间才能检测到这种情况并将其关闭。可能的解决方案(按复杂程度排序):
您似乎担心的另一个问题是 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):
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.
您需要在代码中实现 ACK/NACK 通知。
you need to implement ACK/NACK notification in your code.