簡體   English   中英

如何使用 (a) Smack 庫在 XMPP 上注冊新用戶

[英]How to register a new user on XMPP using (a)Smack library

我已經使用這里的精彩帖子設置了 xmpp 服務器和 android 客戶端......我在 xmpp 服務器中設置了一些預定義的用戶,我可以使用這些憑據登錄。

現在,從我的應用程序中,我想通過 android 客戶端將新用戶注冊為 xmpp 服務器。 任何人都可以建議我如何實現這一目標......任何幫助將不勝感激......!!!

也許我遲到了,但是如果您使用的是最新的smack-android:4.1.0 ,您可以嘗試以下代碼來創建XMPPTCPConnectionConfigurationconnection對象並注冊用戶:

// Creating a connection
XMPPTCPConnectionConfiguration connConfig =
        XMPPTCPConnectionConfiguration.builder()
                .setHost("myHost.com")  // Name of your Host
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setPort(5222)          // Your Port for accepting c2s connection
                .setDebuggerEnabled(true)
                .setServiceName("myServiceName")
                .build();
AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig);

try {
    // connecting...
    connection.connect();
    Log.i("TAG", "Connected to " + connection.getHost());

    // Registering the user
    AccountManager accountManager = AccountManager.getInstance(connection);
    accountManager.sensitiveOperationOverInsecureConnection(true);
    accountManager.createAccount(username, password);   // Skipping optional fields like email, first name, last name, etc..
} catch (SmackException | IOException | XMPPException e) {
    Log.e("TAG", e.getMessage());
    xmppClient.setConnection(null);
}

Smack 具有 InBand 注冊功能,可通過AccountManager類使用。 請注意,並非每個服務器都實現/啟用了此功能。

只是詳細說明Flow發布的內容。 AccountManager類具有在 XMPP 中維護用戶帳戶的所有要素

假設您創建了一個連接對象。

AccountManager accountManager=new AccountManager(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}

為時已晚,但希望它有所幫助

           XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                    .builder();
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setServiceName("nouman.test");
            config.setHost(serverAddress);
            config.setPort(5222);
            config.setDebuggerEnabled(true);
            XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
            XMPPTCPConnection.setUseStreamManagementDefault(true);
            config.setSendPresence(true);
            config.setDebuggerEnabled(true);
            config.setSendPresence(true);
            config.setCompressionEnabled(false);
            connection = new XMPPTCPConnection(config.build());
            connection.connect();


 AccountManager accountManager = AccountManager.getInstance(connection);
        Map<String, String> attributes = new HashMap<>();
        attributes.put("name", "full_name");
        attributes.put("email", "email");
        try {
            if (accountManager.supportsAccountCreation()) {
                accountManager.sensitiveOperationOverInsecureConnection(true);
                accountManager.createAccount("username","password", attributes);
                isAccountCreated = true;
            }
        } catch (Exception e) {
            //TODO : Case 409 or Message conflict is the case of username exist handle the case
            LogUtil.printStackTrace(e);
        }

確保您擁有正確的服務名稱,否則您將收到錯誤的請求錯誤。

如果您使用 smack 4.1.0 或以上版本,請將您的用戶名轉換為 Localpart 以在服務器上創建新帳戶。

 public static void registration(String username,ICallBack iCallBack){

    AccountManager accountManager = AccountManager.getInstance(connection);
    try {
        if(accountManager.supportsAccountCreation()){
            accountManager.sensitiveOperationOverInsecureConnection(true);
            accountManager.createAccount(Localpart.from(username), username);
            iCallBack.onSuccess();
        }
    } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
        iCallBack.onFailure(e);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (XmppStringprepException e) {
        e.printStackTrace();
    }

}

我想更新答案以反映 Asmack 庫 4.0 版以后的變化。 Connection.getAccountManager() 現在是 AccountManager.getInstance(XMPPConnection)

AccountManager accountManager=AccountManager.getInstance(connection);
try {
    accountManager.createAccount("username", "password");
} catch (XMPPException e1) {
    Log.d(e1.getMessage(), e1);
}

我通過開發一個將用戶名和密碼作為發布參數的網絡服務解決了這個問題。 如果我們發布用戶名和密碼,網絡服務就會注冊一個新用戶。

我發現這相當簡單,而不是從應用程序注冊...

如果您使用的是最新版本,請使用此版本。

new Thread() {
                    @Override
                    public void run() {
                        try {
                            AccountManager accountManager = AccountManager.getInstance(mConnection);
                            accountManager.sensitiveOperationOverInsecureConnection(true);
                            Map<String, String> map = new HashMap<String, String>();
                            map.put("username","vinay");
                            map.put("name", "vinay");
                            map.put("password", "vinay");
                            map.put("emial", "vinay@gmail.com");
                            accountManager.createAccount(Localpart.from("vinay"), "vinay", map);
                        } catch (SmackException.NoResponseException e) {
                            e.printStackTrace();
                        } catch (XMPPException.XMPPErrorException e) {
                            e.printStackTrace();
                        } catch (SmackException.NotConnectedException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (XmppStringprepException e) {
                            e.printStackTrace();
                        }

                    }
                }.start();

您需要在客戶端InBand Registration功能中實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM