聊天仅适用于Smack 4.1.7 Android的单向浏览

时间:2016-05-11 13:31:17

标签: android xmpp chat openfire smack

我正在开发基于 Smack 4.1.7 的Android Chat应用。 我已经在Saperate类上创建了我的XMPP相关操作MyXMPP.java。 在我的应用程序的Application类中,我正在初始化MyXMPP类对象。

我的问题是,假设我们有用户1和用户2.如果用户1向用户2发送消息,则用户2可以获得消息但是无法回复。表示如果用户2尝试回复,则用户1无法获得用户2的回复。

简而言之,如果用户1发起聊天,那么只有用户1可以发送消息。用户2无法向用户1发送消息,反之亦然。

我的代码如下,

MyXMPP.java

    public class MyXMPP {

    static Context context;
    static XMPPTCPConnectionConfiguration.Builder xmppConfig;
    static XMPPTCPConnection connection;
    static MyXMPP myXMPP = null;
    static Chat chat;
    Roster roster;
    ArrayList<Rosters> rosters;
    static ChatManager chatManager;

    static Application app;

    public static synchronized MyXMPP getInstance(Context c) {

        app = (Application) c.getApplicationContext();
        context = c;
        if (myXMPP == null) {
            myXMPP = new MyXMPP();
            xmppConfig = XMPPTCPConnectionConfiguration.builder();
            xmppConfig.setResource(Constants.RESOURCE);
            xmppConfig.setServiceName(Constants.SERVICE_NAME);
            xmppConfig.setPort(Constants.PORT);
            xmppConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        }

        return myXMPP;
    }

    public static synchronized AbstractXMPPConnection getConnectXMPP() {

        try {
            xmppConfig.setUsernameAndPassword(Constants.USERNAME, Constants.PASSWORD);
            XMPPTCPConnection.setUseStreamManagementDefault(true);
            connection = new XMPPTCPConnection(xmppConfig.build());
            connection.setUseStreamManagement(true);
            connection.connect().login();
            connection.requestSmAcknowledgement();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

        return connection;
    }

    public static ChatManager getChatManager() {

        if (chatManager == null) {
            chatManager = ChatManager.getInstanceFor(app.connection);
        }
        return chatManager;
    }


    public ArrayList<Rosters> getRosters() {

        rosters = new ArrayList<>();

        roster = Roster.getInstanceFor(app.connection);

        if (!roster.isLoaded())
            try {
                roster.reloadAndWait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (SmackException.NotLoggedInException e) {
                e.printStackTrace();
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            }

        //Get Roster Entry list
        Set<RosterEntry> rosterEntries = roster.getEntries();
        Debug.e("entry size", "" + rosterEntries.size());
        for (final RosterEntry entry : rosterEntries) {
            /*
             build List<> for roster entry with roster
                            ---or---
             if it is in activity or in fragment then add both items (entry,roster) to adapter
             */
            Rosters rosterItem = new Rosters();
            rosterItem.entry.add(entry);
            rosterItem.roster.add(roster);
            rosters.add(rosterItem);
        }

        return rosters;

    }

    public void initChatManager() {

        if (chatManager == null) {
            chatManager = getChatManager();
        }

        chatManager.addChatListener(new ChatManagerListener() {

            @Override
            public void chatCreated(Chat chat, boolean createdLocally) {

                MyXMPP.chat = chat;

                if (!createdLocally) {

                    chat.addMessageListener(new ChatMessageListener() {
                        @Override
                        public void processMessage(Chat chat, Message message) {
                            Debug.e("Chat obj", chat.toString());
                            if (message != null || !message.getBody().equalsIgnoreCase(null) || !message.getBody().equalsIgnoreCase("")) {
                                if (message.getBody() == null || message.getBody().equals(null)) {
                                } else {
                                    Debug.e("Message aala", "" + message.getBody());
                                }

                            } else {
                                Debug.e("message null", "Message Null");
                            }
                        }
                    });
                } else {
                    Debug.e("MY MSG", chat.getParticipant());
                }
            }
        });
    }

    public void sendMessage(String to, String msg) {

        Chat newChat = app.myXMPP.getChatManager().createChat(to, new ChatMessageListener() {
            @Override
            public void processMessage(Chat chat, Message message) {
                System.out.println("Received message: " + message);
            }
        });

        try {
            Message message = new Message();
            message.setFrom(connection.getUser());
            message.setTo("xyz@myhostaddress.com");
            message.setBody("My Message");
            newChat.addMessageListener(new ChatMessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) {
                    Debug.e("send msg listener", message.getBody());
                }
            });
            newChat.sendMessage(message);
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        }
    }
  }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Application app;
    Button btn;
    String threadId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                app.myXMPP.sendMessage("xyz@myhost.com", "hello");
            }
        });

        //Get Application data access
        app = (Application) getApplicationContext();

        //Establish Connection and Login
        new XMPPOperations().execute();
    }

    class XMPPOperations extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if (app.connection == null || !app.connection.isConnected()) {
                app.connection = app.myXMPP.getConnectXMPP();
                Debug.e("Connection in activity", "" + app.connection.isConnected());
            }
            app.myXMPP.getRosters();

            app.myXMPP.initChatManager();

            return null;
        }
    }
}

Application.java

public class Application extends android.app.Application {

    public MyXMPP myXMPP;
    public AbstractXMPPConnection connection;

    @Override
    public void onCreate() {
        super.onCreate();

        myXMPP = MyXMPP.getInstance(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Presence p = new Presence(Presence.Type.unavailable);
        try {
            connection.sendStanza(p);
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案