gpt4 book ai didi

xmpp - 如何在 Smack 中正确收听 MultiUserChat?

转载 作者:行者123 更新时间:2023-12-01 12:40:56 25 4
gpt4 key购买 nike

我不确定如何正确收听 MultiUserChats,因为显然有两种不同的收听方式。

第一种方式是为每个房间添加一个MessageListener,像这样:

MultiUserChat muc = new MultiUserChat(connection,"MyGroup");
muc.addMessageListener(new PacketListener() {
@Override
public void processPacket(Packet packet) throws NotConnectedException {
// TODO Auto-generated method stub
}
});

第二种方法是像收听普通的单聊天消息一样收听:

PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
final String fromName = StringUtils.parseBareAddress(message.getFrom());
final String Rtext = message.getBody();

考虑到我也在监听文件传输,你能帮我看看哪种方式是正确的吗?

最佳答案

由于原始代码都有错误,我使用了这段代码:

     PacketFilter messageFilter =

new AndFilter(

new FromMatchesFilter(roomName+"@conference.reza-hp",true),

new MessageTypeFilter(Message.Type.groupchat));

messageFilter = new AndFilter(messageFilter, new PacketFilter() {

public boolean accept(Packet packet) {

Message msg = (Message) packet;

return msg.getBody() != null;



}

});

addMessageListener(messageFilter);
}

public static void addMessageListener(PacketFilter messageFilter) {

connection.addPacketListener(new PacketListener() {

@Override
public void processPacket(Packet packet) throws NotConnectedException {
Message message = (Message)packet;
String roomName = (message.getFrom().split("\\@conference.reza-
hp"))[0];
String fromUser =
(StringUtils.parseResource(message.getFrom()).split("@\\@reza-hp"))[0];

System.out.println("=>"+"Room : "+roomName + "From
user:"+fromUser + ": "+message.getBody());

}
}, messageFilter);



}

关于xmpp - 如何在 Smack 中正确收听 MultiUserChat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24808798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com