smack 4.1 Openfire Sample example

前端 未结 2 618
感情败类
感情败类 2021-02-04 20:56

I have been using smack 3.4 for my web portal. Also used asmack 3.4 for my android app (aSmack development stopped some years back but there where some unofficial jars that i us

相关标签:
2条回答
  • 2021-02-04 21:44

    We finally moved to Quickblox, as the current version of smack which supports native android is still under beta Phase.

    0 讨论(0)
  • 2021-02-04 21:51

    Example of connection using SSL (smack-4.1.0-beta2-SNAPSHOT-2015-02-01) :

        XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
        //For OLD STYLE SSL
        //config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
        config.setUsernameAndPassword(USERNAME + "@" + DOMAIN, "PASSWORD");
        config.setServiceName(DOMAIN);
        config.setHost(DOMAIN);
        config.setPort(PORT);
        config.setDebuggerEnabled(true);
        //OLD STYLE SSL
        //config.setSocketFactory(SSLSocketFactory.getDefault());
    
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            MemorizingTrustManager mtm = new MemorizingTrustManager(ctx);
            sc.init(null, MemorizingTrustManager.getInstanceList(ctx), new SecureRandom());
            config.setCustomSSLContext(sc);
            config.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IllegalStateException(e);
        }
    
        mConnection = new XMPPTCPConnection(config.build());
        mConnection.setPacketReplyTimeout(10000);
    
        try {
            mConnection.connect();
            mConnection.login();
        } catch (SmackException | IOException | XMPPException e) {
            e.printStackTrace();
        }
    
    • https://github.com/ge0rg/MemorizingTrustManager/tree/master/src/de/duenndns/ssl

    Chat creation example:

               final ChatManager chatManager = ChatManager.getInstanceFor(mConnection);
                chatManager.addChatListener(new ChatManagerListener() {
                    @Override
                    public void chatCreated(Chat chat, boolean b) {
                        chat.addMessageListener(new ChatMessageListener() {
                            @Override
                            public void processMessage(Chat chat, Message message) {
                                mServerResponse.gotMessage(message.getBody());
                                Log.d(TAG, message.toString());
                            }
                        });
                    }
                });
    
                Chat chat2 = chatManager.createChat(USERNAME + "@" + DOMAIN);
                try {
                    chat2.sendMessage("text");
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
    
    0 讨论(0)
提交回复
热议问题