4

私は、Smack と Openfire サーバーを介した XMPP チャットに苦労しています。私の問題は次のとおりです。

ユーザーが別のユーザーにメッセージを送信すると、そのメッセージは別のユーザーで正しく受信されます。しかし、最初のメッセージの送信者には返信が表示されません。したがって、ユーザー 1 はユーザー 2 に正常に送信します。その後、ユーザー 2 はユーザー 1 に返信を送信できなくなります。一方、再起動してユーザーを再度ログインさせると、ユーザー 2 はユーザー 1 に送信できますが、その逆はできません。

私が言おうとしているのは、チャットの開始者のみがメッセージを送信でき、受信者は返信できないということです。

私のコードは次のようになります

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

}

何が問題なのかわからない。助言がありますか?サンプルコード?

ありがとう<3

4

3 に答える 3

1

たとえば、受信者が既存のクライアント (たとえば、Spark など) である場合、またはより多くのカスタム コードである場合、受信者が何であるかを指定していません。これは、使用している Smack のバージョンを知るのに役立ちます。

その特定のコードにはいくつかの問題があります。

  • 同じチャットを単純に再利用するのではなく、メッセージが送信されるたびに新しい Chat オブジェクトを作成し続けます。
  • 既存のチャットに関連付けられていない新しいチャット メッセージを処理するために登録されたChatManagerListenerはありません。
于 2012-05-30T14:39:23.030 に答える
0

is コードは非常に複雑で、メッセージの送信のみを目的としているようです。以下は、送信と受信の両方で完全に機能するサンプル コードです。

http://www.javaprogrammingforums.com/java-networking-tutorials/551-how-write-simple-xmpp-jabber-client-using-smack-api.html

于 2012-05-30T13:12:41.493 に答える