- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力通过 Smack 和 Openfire 服务器与 XMPP 聊天。
我的问题如下:
每当一个用户向另一个用户发送消息时,该消息就会被另一个用户正确接收。但是第一条消息的发件人不会显示任何回复。
所以用户 1 成功发送给用户 2。然后,用户 2 无法向用户 1 发送任何回复。
另一方面,如果我重新启动并让用户再次登录,用户 2 可以发送给用户 1,反之亦然。
我想说的是,只有聊天的发起者才能发送消息,接收者无法回复。
我的代码看起来像 this
package xmpp;
public class XMPPClient{
private static final int packetReplyTimeout = 500; // millis
private XMPPConnection connection;
private ChatManager chatManager;
private MessageListener messageListener;
private ConnectionConfiguration config;
private MyTimer t = MyTimer.getInstance();
private ArrayList<String> threadPool = new ArrayList<String>();
public XMPPClient()
{
SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);
//define openfire server information
config = new ConnectionConfiguration("localhost",5222);
config.setSASLAuthenticationEnabled(false);
config.setSecurityMode(SecurityMode.disabled);
connection = new XMPPConnection(config);
//connect to server
t.start("Connecting to server...");
try {
connection.connect();
} catch (XMPPException e) {
System.err.println("Failed to connect to server! Connect to VPN!\t"+e.getMessage());
System.exit(0);
}
t.end("Connection took ");
//setup chat mechanism
chatManager = connection.getChatManager();
chatManager.addChatListener(
new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally)
{
if (!createdLocally)
chat.addMessageListener(new MyMessageListener());
}
});
}
public boolean login(String userName, String password, String resource) {
t.start("Logging in...");
try {
if (connection!=null && connection.isConnected())
connection.login(userName, password, resource);
//set available presence
setStatus(true);
}
catch (XMPPException e) {
if(e.getMessage().contains("auth")){
System.err.println("Invalid Login Information!\t"+e.getMessage());
}
else{
e.printStackTrace();
}
return false;
}
t.end("Logging in took ");
return true;
}
public void setStatus(boolean available) {
if(available)
connection.sendPacket(new Presence(Presence.Type.available));
else
connection.sendPacket(new Presence(Presence.Type.unavailable));
}
public void sendMessage(String message, String buddyJID) throws XMPPException {
System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
boolean chatExists = false;
Chat c = null;
for(String tid : threadPool)
{
if((c = chatManager.getThreadChat(tid)) != null)
{
if(c.getParticipant().equals(buddyJID))
{
if(checkAvailability(buddyJID))
{
chatExists = true;
break;
}
else
{
threadPool.remove(tid);
break;
}
}
}
}
if (chatExists)
{
Chat chat = c;
chat.sendMessage(message);
}
else
{
Chat chat = chatManager.createChat(buddyJID, messageListener);
threadPool.add(chat.getThreadID()); System.out.println(chat.getThreadID());
chat.sendMessage(message);
}
}
public void createEntry(String user, String name) throws Exception {
System.out.println(String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
Roster roster = connection.getRoster();
roster.createEntry(user, name, null);
}
public boolean checkAvailability(String jid)
{
System.out.print("Checking availability for: "+jid+"=");
System.out.println(connection.getRoster().getPresence(jid).isAvailable());
return connection.getRoster().getPresence(jid).isAvailable();
}
public void disconnect() {
if (connection!=null && connection.isConnected()) {
setStatus(false);
connection.disconnect();
}
}
import org.jivesoftware.smack.packet.Message;
public class MyMessageListener implements MessageListener {
@Override
public void processMessage(Chat chat, Message message) {
String from = message.getFrom();
String body = message.getBody();
System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
}
最佳答案
我不确定这是否会对您有所帮助,但我可以通过以下代码得到回复:
public void chat(String AddressedUser) throws NotConnectedException {
//Create username whom we want to send a message
String userToSend = AddressedUser + "@" + serverDomain;
ChatManager chatmanager = ChatManager.getInstanceFor(connection);
Chat newChat = chatmanager.createChat(userToSend , new MessageListener() {
@Override
public void processMessage(Chat chat, Message message ) {
// TODO Auto-generated method stub
System.out.println("Received message: " + message);
}
});
try {
newChat.sendMessage("Hey");
}
catch (XMPPException e) {
System.out.println("Error Delivering block");
}
}
关于XMPP:Smack 客户端未收到聊天消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10808956/
我使用 Smack API 编写我的 Google talk Client 。现在我需要处理这个添加请求。 我将 SubscriptionMode 设置为手动,现在我必须为存在数据包注册一个监听器,但
我正在尝试从应用程序服务器(TOMCAT)创建多用户聊天组,我已经在servlet中创建了一个XMPP机器人。 根据我的要求,我必须从应用程序服务器创建一个多用户聊天组(在应用程序服务器上使用 xmp
我正在将我的 android 应用程序从 asmack-android 库移动到 Smack 4.1.4。我在 Smack 的 asmack 版本中有一些 PacketExtensions,它使用 P
我正在使用 Smack API 开发多用户游戏客户端。我的 smack 应用程序遇到问题:我可以向服务器发送消息和请求,但我的应用程序无法从中读取响应。 Exception in thread "Sm
经过数小时的调试并试图找出为什么使用 aSmack 无法进行文件传输,而正常的消息传递却可以,我终于设法将其归结为这一点。 当我按照 Smack 文档中给出的方法获取用户的名册列表时,Openfire
我是 gcm 新手,我尝试使用 Smack API 连接到 GCM 的云连接服务器(XMPP)。一开始还好我的代码: uid = "123456789"; apiKey = "A***********
相关问题: Initialization exception "NoClassDefFoundError: javax.naming.directory.InitialDirContext" when
如何订阅以收听 SMACK 中的用户可用性状态变化? 要获取用户的可用性状态,我使用以下命令: XMPPConnection.getRoster().getPresence(name).isAvail
我知道之前有人问过这个问题,但 mschonaker here 只给出了部分答复.在我的网站上,用户可以将人添加到他们的花名册中,然后好友必须接受并最终连接。第一人称(用户a)使用著名的 roster
我正在为 openfire 聊天客户端创建一个 java fx 应用程序。我正在使用 smack 4.1 rc1 连接到服务器。我能够连接到服务器,向其他人发送存在信息,并向其他用户发送消息。但是我无
我正在为 openfire 聊天客户端创建一个 java fx 应用程序。我正在使用 smack 4.1 rc1 连接到服务器。我能够连接到服务器,向其他人发送存在信息,并向其他用户发送消息。但是我无
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在邀请离线用户加入群组,如下所示, myChattingRoom.invite("user7@10.10.1.105", "请加入我的聊天室"); 由于邀请消息默认没有正文,因此 openfire
我一直在努力通过 Smack 和 Openfire 服务器与 XMPP 聊天。 我的问题如下: 每当一个用户向另一个用户发送消息时,该消息就会被另一个用户正确接收。但是第一条消息的发件人不会显示任何回
我在 Java 的 Smack 消息传递库中建立适当的聊天时遇到问题。如果聊天是在本地开始的,它工作得很好,但如果聊天是从另一个客户端开始的,那么会创建两个不同的聊天线程,而不仅仅是需要的那个。我发送
我正在使用下面的代码向任何用户发送好友请求,它工作正常,但我无法从花名册列表中找到已连接的用户。 现在我想知道如何在花名册列表中识别新请求的 friend 。 Roster roster = XMPP
我正在使用 smack 和 openfire 在 android 中创建聊天应用程序。对于消息状态,我在其他地方发送和显示的消息没有问题客户(仔细检查)。我将向发件人发送一条简单的 json 消息,如
嗨, friend 们,我正在使用从以下网址下载的 Smack3.2.1.jar http://www.igniterealtime.org/downloads/index.jsp 我在我的构建路径中
我正在尝试为 asmack 数据包编写一个监听器。最终发生的事情是监听器有时只是没有获取数据包并对其进行处理。以下是部分代码: try { XMPPMethods.getCon
我有以下代码来监听聊天室的邀请: Connection.DEBUG_ENABLED = true; XMPPConnection connection = new XMPPConnec
我是一名优秀的程序员,十分优秀!