在初始广播消息之后将消息发送到特定的Smack域

时间:2022-05-24 19:53:21

I'm creating an instant messaging client using Smack 3.1.0 and Java. The problem I'm running in to has to do with sending messages to the user on a specific domain.

我正在使用Smack 3.1.0和Java创建一个即时消息客户端。我遇到的问题与在特定域上向用户发送消息有关。

For example, I have two users, 1@gmail.com and 2@gmail.com. 1@gmail.com logs in to XMPP through my IM client. 2@gmail.com logs in to GChat through gmail.com AND a second time through pidgin. So now I have one instance of 1@gmail.com and 2 instances of 2@gmail.com.

例如,我有两个用户,1 @ gmail.com和2@gmail.com。 1@gmail.com通过我的IM客户端登录XMPP。 2@gmail.com通过gmail.com登录到GChat,第二次通过pidgin登录。所以现在我有一个1@gmail.com实例和2个实例2@gmail.com。

The way gmail works, if 1@gmail.com sends a message to 2@gmail.com, the gmail and the pidgin client both get the initial message. But then if the gmail instance responds to the message, every message from then on only goes between 1@gmail.com and the gmail instance of 2@gmail.com.

gmail的工作方式,如果1 @gmail.com向2@gmail.com发送消息,gmail和pidgin客户端都会收到初始消息。但是,如果gmail实例响应该消息,那么从那时开始的每条消息都只在1@gmail.com和2@gmail.com的gmail实例之间。

I would like to mimic this behavior with my IM client. I would think the way to do it would be to set up a Chat, send the initial IM to all instances of the recipient. Then I'd set up a MessageListener to listen for a response. When I get the response, I'd have to create a new chat, specifying the 2@gmail.com/resource. But then I'd have to write the MessageListener twice. Any ideas? Here's some sample code that I'm using (the method AddText() simply appends the message to my conversation pane):

我想用我的IM客户端模仿这种行为。我认为这样做的方法是设置聊天,将初始IM发送给收件人的所有实例。然后我设置一个MessageListener来监听响应。当我收到回复时,我必须创建一个新的聊天,指定2@gmail.com/resource。但后来我必须写两次MessageListener。有任何想法吗?这是我正在使用的一些示例代码(方法AddText()只是将消息附加到我的对话窗格):

recipient = buddy;
setTitle("Instant Message - "+recipient);
chat = com.andreaslekas.pim.PIM.connection.getChatManager().createChat(recipient.getUser(), new MessageListener() {
    public void processMessage(Chat chat, Message msg) {
        //if(chat.getParticipant().indexOf('/')!=-1)
        addText(msg.getBody(), chat.getParticipant(), true);
    }
});

UPDATE I wanted to supplement the answer below with actual code that I used to make this work:

更新我想用下面的答案来补充我用来完成这项工作的实际代码:

chat = com.andreaslekas.pim.PIM.connection.getChatManager().createChat(recipient.getUser(), new MessageListener() {
    public void processMessage(Chat new_chat, Message msg) {
        if(msg.getFrom().replaceFirst("/.*", "").equals(recipient.getUser()))
        {
            if(buddy_resource==null || !msg.getFrom().replaceFirst(".*?/", "").equals(buddy_resource.getResource()))
            {
                buddy_resource = recipient.getResource(msg.getFrom().replaceFirst(".*?/", ""));
                chat = null;
                chat = com.andreaslekas.pim.PIM.connection.getChatManager().createChat(recipient.getUser()+"/"+buddy_resource.getResource(), new MessageListener(){
                    public void processMessage(Chat new_chat2, Message msg) {
                        addText(msg.getBody(), new_chat2.getParticipant(), true);
                    }
                });
            }
            addText(msg.getBody(), chat.getParticipant(), true);
        }
    }
});

To summarize, I send the first message to all resources of the recipient's address and wait for a response. When I get the response, I replace the current Chat object with a new one that specifies the individual resource that responded to the initial message. The code is a little messy with two different MessageListener objects that could probably be combined into a new class. But it works.

总而言之,我将第一条消息发送到收件人地址的所有资源并等待响应。当我得到响应时,我用一个新的替换当前的Chat对象,该对象指定响应初始消息的单个资源。代码有点混乱,有两个不同的MessageListener对象,可能组合成一个新类。但它的确有效。

2 个解决方案

#1


0  

In your MessageListener why not always respond to the sender? I think you get it by calling something like msg.getSender() or getFrom() (I'm on mobile right now, cannot check)

在MessageListener中,为什么不总是回复发件人?我想你通过调用msg.getSender()或getFrom()之类的东西得到它(我现在在移动设备上,无法检查)

#2


0  

So far I understood Message Carbon (XEP - 0280) will solve your problem. If you enable carbon it will distribute messages to all logged resources of a user. In your case if 1@gmail.com send message to 2@gmail.com it will be distributed to all logged resources of 2@gmail.com. Here's a code sample using smack,

到目前为止,我了解Message Carbon(XEP-0280)将解决您的问题。如果启用carbon,它会将消息分发给用户的所有已记录资源。在您的情况下,如果1@gmail.com发送消息到2@gmail.com,它将被分发到2@gmail.com的所有记录资源。这是使用smack的代码示例,

CarbonManager cm = CarbonManager.getInstanceFor(connection);
cm.enableCarbons();
cm.sendCarbonsEnabled();

First make sure that your server is supported Message Carbon. Then send message as usual.

首先确保您的服务器支持Message Carbon。然后像往常一样发送消息。

#1


0  

In your MessageListener why not always respond to the sender? I think you get it by calling something like msg.getSender() or getFrom() (I'm on mobile right now, cannot check)

在MessageListener中,为什么不总是回复发件人?我想你通过调用msg.getSender()或getFrom()之类的东西得到它(我现在在移动设备上,无法检查)

#2


0  

So far I understood Message Carbon (XEP - 0280) will solve your problem. If you enable carbon it will distribute messages to all logged resources of a user. In your case if 1@gmail.com send message to 2@gmail.com it will be distributed to all logged resources of 2@gmail.com. Here's a code sample using smack,

到目前为止,我了解Message Carbon(XEP-0280)将解决您的问题。如果启用carbon,它会将消息分发给用户的所有已记录资源。在您的情况下,如果1@gmail.com发送消息到2@gmail.com,它将被分发到2@gmail.com的所有记录资源。这是使用smack的代码示例,

CarbonManager cm = CarbonManager.getInstanceFor(connection);
cm.enableCarbons();
cm.sendCarbonsEnabled();

First make sure that your server is supported Message Carbon. Then send message as usual.

首先确保您的服务器支持Message Carbon。然后像往常一样发送消息。