public ReturnState login(String email, String password) { try { // attempt to connect this.xmppConn.connect(); // extract JID from the email address by removing nonalphanumeric // characters from the email address String jid = email.replaceAll("[^a-zA-Z0-9]", "") + DEFAULT_HOSTNAME; if (D) Log.d( TAG, "Attempt login: email - " + email + ", jid - " + jid + ", password - " + password); try { this.xmppConn.login(jid, password, DEFAULT_RESOURCE); // check that the email is the right one if (!email.equals(this.getConnection().getAccountManager().getAccountAttribute("email"))) { if (D) Log.d(TAG, "Email does not match"); // disconnect this.xmppConn.disconnect(); // reconnect to the server _instance = new NetworkService(DEFAULT_HOST, DEFAULT_PORT); MultiUserChat.addInvitationListener(NetworkService.getInstance().getConnection(), null); return ReturnState.INVALID_PAIR; } return ReturnState.SUCCEEDED; } catch (XMPPException e) { // if login failed e.printStackTrace(); return ReturnState.INVALID_PAIR; } } catch (XMPPException e) { if (D) Log.d(TAG, "Connection to server failed"); return ReturnState.COULDNT_CONNECT; } }
/** 被邀请监听 */ private void setInviterListener() { Log.i(TAG, "创建的服务监听"); MultiUserChat.addInvitationListener( Constants.conn, new InvitationListener() { // 对应参数:连接、 房间JID、房间名、附带内容、密码、消息 @Override public void invitationReceived( Connection conn, String room, String inviter, String reason, String password, Message message) { Log.i(TAG, "收到来自 " + inviter + " 的聊天室邀请。邀请附带内容:" + reason); Intent intent = new Intent(MucService.this, ActivityMultiRoom.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("jid", room); intent.putExtra("action", "join"); startActivity(intent); } }); }
/** * Creates a new workgroup instance using the specified workgroup JID (eg * [email protected]) and XMPP connection. The connection must have undergone a * successful login before being used to construct an instance of this class. * * @param workgroupJID the JID of the workgroup. * @param connection an XMPP connection which must have already undergone a successful login. */ public Workgroup(String workgroupJID, Connection connection) { // Login must have been done before passing in connection. if (!connection.isAuthenticated()) { throw new IllegalStateException("Must login to server before creating workgroup."); } this.workgroupJID = workgroupJID; this.connection = connection; inQueue = false; invitationListeners = new ArrayList<WorkgroupInvitationListener>(); queueListeners = new ArrayList<QueueListener>(); // Register as a queue listener for internal usage by this instance. addQueueListener( new QueueListener() { public void joinedQueue() { inQueue = true; } public void departedQueue() { inQueue = false; queuePosition = -1; queueRemainingTime = -1; } public void queuePositionUpdated(int currentPosition) { queuePosition = currentPosition; } public void queueWaitTimeUpdated(int secondsRemaining) { queueRemainingTime = secondsRemaining; } }); /** * Internal handling of an invitation.Recieving an invitation removes the user from the queue. */ MultiUserChat.addInvitationListener( connection, new org.jivesoftware.smackx.muc.InvitationListener() { public void invitationReceived( Connection conn, String room, String inviter, String reason, String password, Message message) { inQueue = false; queuePosition = -1; queueRemainingTime = -1; } }); // Register a packet listener for all the messages sent to this client. PacketFilter typeFilter = new PacketTypeFilter(Message.class); connection.addPacketListener( new PacketListener() { public void processPacket(Packet packet) { handlePacket(packet); } }, typeFilter); }