/** * 加入聊天室 * @param xmppConnection * @param roomName * @param password * @return */ public static MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){ try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // 用户加入聊天室 muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); if(packetListener!=null){ muc.addMessageListener(packetListener); } return muc; } catch (XMPPException e) { e.printStackTrace(); return null; } }
/** * 加入聊天室 * @param xmppConnection * @param roomName * @param password * @param packetListener 消息监听器 * @return */ public static MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){ try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // history.setSince(new Date()); // 用户加入聊天室 muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); Log.i("MultiUserChat", "会议室【"+roomName+"】加入成功........"); if(packetListener!=null){ muc.addMessageListener(packetListener); } return muc; } catch (XMPPException e) { Log.e("MultiUserChat", "会议室【"+roomName+"】加入失败........"); e.printStackTrace(); return null; } }
public void joinPublicChat(String chatJid, long lastMessageDate) { try { MultiUserChat mucChat; if ((mucChat = publicChats.get(chatJid)) == null) { mucChat = MultiUserChatManager.getInstanceFor(privateChatConnection).getMultiUserChat(chatJid); mucChat.addMessageListener(this); publicChats.put(chatJid, mucChat); } DiscussionHistory history = new DiscussionHistory(); if(lastMessageDate != 0) history.setSince(new Date(lastMessageDate * 1000)); else history.setMaxChars(0); mucChat.join( CurrentUser.getInstance().getCurrentUserId().toString(), null, history, privateChatConnection.getPacketReplyTimeout()); } catch (Exception ex) { Logger.logExceptionToFabric(ex); } }
public void reconnectAll () { MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(mConnection); mucMgr.setAutoJoinOnReconnect(true); Enumeration<MultiUserChat> eMuc = mMUCs.elements(); while (eMuc.hasMoreElements()) { MultiUserChat muc = eMuc.nextElement(); MultiUserChat reMuc = mucMgr.getMultiUserChat(muc.getRoom()); try { DiscussionHistory history = new DiscussionHistory(); reMuc.join(Resourcepart.from(mUser.getName()), null, history, SmackConfiguration.getDefaultPacketReplyTimeout()); mMUCs.put(muc.getRoom().toString(),reMuc); ChatGroup group = mGroups.get(muc.getRoom().toString()); addMucListeners(reMuc, group); } catch (Exception e) { Log.w(TAG,"unable to join MUC: " + e.getMessage()); } } }
public void start(ITetradCallback callback) { logger.debug("start"); logger.info(MessageFormat.format("Connecting to service {0} with username {1} and resource {2}", service_domain, username, resource )); this.callback = callback; try { XMPPConnection xmppConnection = new XMPPConnection(service_domain); xmppConnection.connect(); xmppConnection.login(username, password, resource); DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(0); for (String room : rooms) { final String roomJID = room + "@" + getChatService(); logger.info(MessageFormat.format("Connecting to room {0}", roomJID)); MultiUserChat chat = new MultiUserChat(xmppConnection, roomJID); chat.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); chat.addMessageListener(packet -> this.handleMessage((Message) packet, roomJID)); chat.changeNickname("β"); connectedRooms.put(room, chat); } } catch (XMPPException e) { logger.error(MessageFormat.format("Error connecting to service {0} with username {1} and resource {2}. Message: {3}", service_domain, username, resource, e.getMessage() )); } }
/** * ���������� * * @param user * @param pwd * ���������� * @param roomName * @return */ public MultiUserChat joinRoom(String user, String pwd, String roomName) { MultiUserChat muc = new MultiUserChat(connection, roomName.contains(CONFERENCE) ? roomName : roomName + CONFERENCE + connection.getServiceName()); DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(100); history.setSince(new Date(2014, 07, 31)); // history.setSince(new Date()); try { muc.join(user, pwd, history, SmackConfiguration.getPacketReplyTimeout()); Message msg = muc.nextMessage(); if (null != msg) SLog.i(tag, msg.toXML()); Message msg2 = muc.nextMessage(100); if (null != msg2) SLog.i(tag, msg2.toXML()); } catch (XMPPException e) { SLog.e(tag, " ���� ������ ����"); SLog.e(tag, Log.getStackTraceString(e)); return null; } return muc; }
@Override protected void doStart() throws Exception { try { connection = endpoint.createConnection(); } catch (SmackException e) { if (endpoint.isTestConnectionOnStartup()) { throw new RuntimeException("Could not connect to XMPP server.", e); } else { LOG.warn(e.getMessage()); if (getExceptionHandler() != null) { getExceptionHandler().handleException(e.getMessage(), e); } scheduleDelayedStart(); return; } } chatManager = ChatManager.getInstanceFor(connection); chatManager.addChatListener(this); OrFilter pubsubPacketFilter = new OrFilter(); if (endpoint.isPubsub()) { //xep-0060: pubsub#notification_type can be 'headline' or 'normal' pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.headline)); pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.normal)); connection.addPacketListener(this, pubsubPacketFilter); } if (endpoint.getRoom() == null) { privateChat = chatManager.getThreadChat(endpoint.getChatId()); if (privateChat != null) { if (LOG.isDebugEnabled()) { LOG.debug("Adding listener to existing chat opened to " + privateChat.getParticipant()); } privateChat.addMessageListener(this); } else { privateChat = ChatManager.getInstanceFor(connection).createChat(endpoint.getParticipant(), endpoint.getChatId(), this); if (LOG.isDebugEnabled()) { LOG.debug("Opening private chat to " + privateChat.getParticipant()); } } } else { // add the presence packet listener to the connection so we only get packets that concerns us // we must add the listener before creating the muc final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class)); connection.addPacketListener(this, packetFilter); muc = new MultiUserChat(connection, endpoint.resolveRoom(connection)); muc.addMessageListener(this); DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // we do not want any historical messages muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getDefaultPacketReplyTimeout()); if (LOG.isInfoEnabled()) { LOG.info("Joined room: {} as: {}", muc.getRoom(), endpoint.getNickname()); } } this.startRobustConnectionMonitor(); super.doStart(); }
/** * 加入会议室 * * @param user * 昵称 * @param password * 会议室密码 * @param roomsName * 会议室名 */ public static MultiUserChat joinMultiUserChat(Context context, String user, String roomJid, String password) throws Exception { XMPPConnection connection = XmppConnectionUtils.getInstance() .getRawConnection(); if (connection == null || !connection.isConnected() || !connection.isAuthenticated()) throw new Exception("connection not ready"); Cursor cursor = null; try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(connection, roomJid); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); cursor = context.getContentResolver().query( MultiChatRoomColumns.CONTENT_URI, new String[] { MultiChatRoomColumns.LAST_MSG_TIME }, MultiChatRoomColumns.ROOM_JID + "='" + roomJid + "' and " + MultiChatRoomColumns.OWNER + "='" + UserInfo.getUserInfo(context).getUser() + "'", null, null); if (cursor != null && cursor.getCount() == 1) { cursor.moveToFirst(); history.setSince(new Date(cursor.getLong(0))); } else { history.setMaxStanzas(15); } // 用户加入聊天室 muc.join(StringUtils.escapeUserHost(user), password, history, SmackConfiguration.getPacketReplyTimeout()); return muc; } catch (Exception e) { throw e; } finally { if (cursor != null) { cursor.close(); cursor = null; } } }
@Override public void joinChatGroupAsync(Address address, String reason) { String chatRoomJid = address.getBareAddress(); String[] parts = chatRoomJid.split("@"); String room = parts[0]; try { // Create a MultiUserChat using a Connection for a room MultiUserChatManager mucMgr = MultiUserChatManager.getInstanceFor(mConnection); mucMgr.setAutoJoinOnReconnect(true); MultiUserChat muc = mucMgr.getMultiUserChat( JidCreate.entityBareFrom(chatRoomJid)); DiscussionHistory history = new DiscussionHistory(); muc.join(Resourcepart.from(mUser.getName()), null, history, SmackConfiguration.getDefaultPacketReplyTimeout()); String subject = muc.getSubject(); if (TextUtils.isEmpty(subject)) subject = room; ChatGroup chatGroup = mGroups.get(chatRoomJid); if (chatGroup == null) { chatGroup = new ChatGroup(address, subject, this); mGroups.put(chatRoomJid, chatGroup); } mMUCs.put(chatRoomJid, muc); addMucListeners(muc, chatGroup); } catch (Exception e) { debug(TAG,"error joining MUC",e); } }