출처 : http://forum.theorex.tech/t/xmpp-register-login-and-chat-simple-example/198


compile 'org.igniterealtime.smack:smack-android:4.2.0' compile 'org.igniterealtime.smack:smack-tcp:4.2.0' compile 'org.igniterealtime.smack:smack-im:4.2.0' compile 'org.igniterealtime.smack:smack-android-extensions:4.2.0'


public void getSrvDeliveryManager(Context context){ ServiceDiscoveryManager sdm = ServiceDiscoveryManager .getInstanceFor(XMPP.getInstance().connectAndLogin( context)); //sdm.addFeature("http://jabber.org/protocol/disco#info"); //sdm.addFeature("jabber:iq:privacy"); sdm.addFeature("jabber.org/protocol/si"); sdm.addFeature("http://jabber.org/protocol/si"); sdm.addFeature("http://jabber.org/protocol/disco#info"); sdm.addFeature("jabber:iq:privacy"); } public String getUserLocalPart(Context context){ return connectAndLogin(context).getUser().getLocalpart().toString(); } public EntityFullJid getUser(Context context){ return connectAndLogin(context).getUser(); } public Chat getThreadChat(Context context, String party1, String party2){ Chat chat = ChatManager.getInstanceFor( XMPP.getInstance().connectAndLogin(context)) .getThreadChat(party1 + "-" + party2); return chat; } public Chat createChat(Context context, EntityJid jid, String party1, String party2, ChatMessageListener messageListener){ Chat chat = ChatManager.getInstanceFor( XMPP.getInstance().connectAndLogin(context)) .createChat(jid, party1 + "-" + party2, messageListener); return chat; } public void sendPacket(Context context, Stanza packet){ try { connectAndLogin(context).sendStanza(packet); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }


=======================================================================================================================================



private UserLoginTask mAuthTask = null; private ChatManagerListener chatListener; private Chat chat; private Jid opt_jid; private ChatMessageListener messageListener; private StanzaListener packetListener; private boolean register(final String paramString1,final String paramString2) { try { XMPP.getInstance().register(paramString1, paramString2); return true; } catch (XMPPException localXMPPException) { localXMPPException.printStackTrace(); } catch (SmackException.NoResponseException e) { e.printStackTrace(); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } return false; } private boolean login(final String user,final String pass,final String username) { try { XMPP.getInstance().login(user, pass, username); sendBroadcast(new Intent("liveapp.loggedin")); return true; } catch (Exception e) { e.printStackTrace(); try { XMPP.getInstance() .login(user, pass, username); sendBroadcast(new Intent("liveapp.loggedin")); return true; } catch (XMPPException e1) { e1.printStackTrace(); } catch (SmackException e1) { e1.printStackTrace(); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); }catch (Exception e1){ e1.printStackTrace(); } } return false; } public class UserLoginTask extends AsyncTask<Void, Void, Boolean> { public UserLoginTask() { } protected Boolean doInBackground(Void... paramVarArgs) { String mEmail = "abc"; String mUsername = "abc"; String mPassword = "welcome"; if (register(mEmail, mPassword)) { try { XMPP.getInstance().close(); } catch (Exception e) { e.printStackTrace(); } } return login(mEmail, mPassword, mUsername); } protected void onCancelled() { mAuthTask = null; } @Override protected void onPreExecute() { super.onPreExecute(); } protected void onPostExecute(Boolean success) { mAuthTask = null; try { if (success) { messageListener = new ChatMessageListener() { @Override public void processMessage(Chat chat, Message message) { // here you will get only connected user by you } }; packetListener = new StanzaListener() { @Override public void processPacket(Stanza packet) throws SmackException.NotConnectedException, InterruptedException { if (packet instanceof Message) { final Message message = (Message) packet; // here you will get all messages send by anybody } } }; chatListener = new ChatManagerListener() { @Override public void chatCreated(Chat chatCreated, boolean local) { onChatCreated(chatCreated); } }; try { String opt_jidStr = "abc"; try { opt_jid = JidCreate.bareFrom(Localpart.from(opt_jidStr), Domainpart.from(HOST)); } catch (XmppStringprepException e) { e.printStackTrace(); } String addr1 = XMPP.getInstance().getUserLocalPart(getActivity()); String addr2 = opt_jid.toString(); if (addr1.compareTo(addr2) > 0) { String addr3 = addr2; addr2 = addr1; addr1 = addr3; } chat = XMPP.getInstance().getThreadChat(getActivity(), addr1, addr2); if (chat == null) { chat = XMPP.getInstance().createChat(getActivity(), (EntityJid) opt_jid, addr1, addr2, messageListener); PurplkiteLogs.logInfo(TAG, "chat value single chat 1 :" + chat); } else { chat.addMessageListener(messageListener); PurplkiteLogs.logInfo(TAG, "chat value single chat 2:" + chat); } } catch (Exception e) { e.printStackTrace(); } XMPP.getInstance().addStanzaListener(getActivity(), packetListener); XMPP.getInstance().addChatListener(getActivity(), chatListener); XMPP.getInstance().getSrvDeliveryManager(getActivity()); } else { } } catch (Exception e) { e.printStackTrace(); } } } /** * user attemptLogin for xmpp * */ private void attemptLogin() { if (mAuthTask != null) { return; } boolean cancel = false; View focusView = null; if (cancel) { focusView.requestFocus(); } else { try { mAuthTask = new UserLoginTask(); mAuthTask.execute((Void) null); } catch (Exception e) { } } } void onChatCreated(Chat chatCreated) { if (chat != null) { if (chat.getParticipant().getLocalpart().toString().equals( chatCreated.getParticipant().getLocalpart().toString())) { chat.removeMessageListener(messageListener); chat = chatCreated; chat.addMessageListener(messageListener); } } else { chat = chatCreated; chat.addMessageListener(messageListener); } } private void sendMessage(String message) { if (chat != null) { try { chat.sendMessage(message); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); try { XMPP.getInstance().removeChatListener(getActivity(), chatListener); if (chat != null && messageListener != null) { XMPP.getInstance().removeStanzaListener(getActivity(), packetListener); chat.removeMessageListener(messageListener); } } catch (Exception e) { e.printStackTrace(); } }

'Android > xmpp' 카테고리의 다른 글

xmpp smack 이용해서 pubsub 등록하기 예제  (0) 2017.01.25
openfire ssl 설정방법  (0) 2017.01.23
XMPP 그룹 채팅 흐름도..  (0) 2017.01.17
Xmpp 으로 할수 있는 내용 정리  (0) 2017.01.17
Android xmpp  (0) 2017.01.16

+ Recent posts