我不确定如何正确收听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();
考虑到我也在听文件传输,你能帮我找出正确的方法吗?
答案 0 :(得分:3)
由于原始代码都有错误,我使用此代码:
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);
}
答案 1 :(得分:1)
MultiUserChat muc = new MultiUserChat(connection,"MyGroup"); muc.addMessageListener(new PacketListener() {
@Override
public void processPacket(Packet packet) throws NotConnectedException {
// TODO Auto-generated method stub
} });
是正确的方法。