android IM的实现:聊天室(采用smack API)

由于Smack的API版本不一样吧,以为会有个对应的GroupChat来处理聊天室(跟Chat处理普通聊天一样).可我错了,我没有找到这个类

翻了下API,最后发现只有采用MultiUserChat

获取服务器端聊天室信息:

public static void getConferenceRoom() throws XMPPException {
		if (!MultiUserChat.getHostedRooms(mAdaptee, "127.0.0.1").isEmpty()) {
			for (HostedRoom k : MultiUserChat.getHostedRooms(mAdaptee,
					"127.0.0.1")) {
				for (HostedRoom j : MultiUserChat.getHostedRooms(mAdaptee,
						k.getJid())) {
					RoomInfo info2 = MultiUserChat.getRoomInfo(mAdaptee,
							j.getJid());
					if (j.getJid().indexOf("@") > 0) {
						System.out.println("聊天室的名称是: " + j.getName()
								+ "\n JID是:" + j.getJid());
						System.out.println("aaNumber of occupants:"
								+ info2.getOccupantsCount());
						System.out
								.println("聊天室的描述:" + info2.getDescription());
						System.out.println("聊天室的主题:" + info2.getSubject());
						System.out.println("聊天室的JID:" + info2.getRoom());
					}
					
				}
			}
		}
		
	}

加入聊天室,获取聊天室的人数,监听消息的发送与接收,监听聊天室的动态:

public static void joinRoom() throws XMPPException{
		MultiUserChat multiUserChat = new MultiUserChat(mAdaptee, "minzujy@conference.127.0.0.1");
		multiUserChat.join("NiMei", "");
		multiUserChat.sendMessage("send message successful");// 发送消息
		
		System.out.println(multiUserChat.getOccupantsCount());// 聊天室人数
		
		Iterator<String> it = multiUserChat.getOccupants();
		while(it.hasNext()){
			// minzujy@conference.127.0.0.1/sushuo1
			// minzujy@conference.127.0.0.1/guohai
			System.out.println(StringUtils.parseResource(it.next()));// 聊天室成员名字
		}
		
		multiUserChat.addMessageListener(new PacketListener() {
			@Override
			public void processPacket(Packet packet) {
				Message message = (Message)packet;
				//接收来自聊天室的聊天信息
				System.out.println("收到聊天室消息=>" + StringUtils.parseResource(message.getFrom()) + ": "+message.getBody());
			}
		});
		
		multiUserChat.addParticipantStatusListener(new ParticipantStatusListener() {
			
			@Override
			public void voiceRevoked(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void voiceGranted(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void ownershipRevoked(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void ownershipGranted(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void nicknameChanged(String participant, String newNickname) {
				// TODO Auto-generated method stub
				System.out.println(StringUtils.parseResource(participant) + " is now known as "+ newNickname +".");
			}
			
			@Override
			public void moderatorRevoked(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void moderatorGranted(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void membershipRevoked(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void membershipGranted(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void left(String participant) {
				// TODO Auto-generated method stub
				System.out.println(StringUtils.parseResource(participant) + " has left the room.");
			}
			
			@Override
			public void kicked(String participant, String actor, String reason) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void joined(String participant) {
				// TODO Auto-generated method stub
				System.out.println(StringUtils.parseResource(participant) + " has joined the room.");
			}
			
			@Override
			public void banned(String participant, String actor, String reason) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void adminRevoked(String participant) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void adminGranted(String participant) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

监听好友发送来的加入聊天室的邀请:

MultiUserChat.addInvitationListener(mAdaptee, new InvitationListener() {
			
			@Override
			public void invitationReceived(org.jivesoftware.smack.Connection conn, String room,
					String inviter, String reason, String password, Message message) {
				MultiUserChat multiUserChat = new MultiUserChat(conn, room);
				System.out.println("收到来自 "+inviter+" 的聊天室邀请。邀请附带内容:"+reason);
				try {
					multiUserChat.join("HMM", password);
				} catch (XMPPException e) {
					System.out.println("加入聊天室失败");
					e.printStackTrace();
				}
				System.out.println("成功加入聊天室");
				multiUserChat.addMessageListener(new PacketListener() {
					@Override
					public void processPacket(Packet packet) {
						Message message = (Message)packet;
						//接收来自聊天室的聊天信息
						System.out.println(message.getFrom() + ":" + message.getBody());
					}
				});
			}
		});



展开阅读全文

没有更多推荐了,返回首页