在(a)Smack 4.0中Packet.setProperty()发生了什么变化?

时间:2014-06-16 10:13:16

标签: android smack asmack

您好我最近从aSmack 3.X转换为aSmack 4.0。在aSmack 4.0中,Packet.setProperty()方法似乎已被删除。是否有任何文档或示例如何在aSmack 4.0中将此设置返回到数据包,因为我的系统严重依赖于aSmack属性。

提前谢谢。

2 个答案:

答案 0 :(得分:3)

那些使用Smack 4将smack-core移出smack-extensions的方法。现在需要使用JivePropertiesManager.getProperty(Packet, String)JivePropertiesManager.addProperty(Packet, String)来添加和获取它们。

有关从Smack 3到Smack 4的API更改的更多信息,请访问官方"Smack 4.0 Readme and Upgrade Guide"

答案 1 :(得分:0)

这是我上一次FCM XMPP连接服务器应用程序的升级。现在,这个项目在Smack库(4.1.8)的这个时候使用了最新版本。

https://github.com/carlosCharz/fcmxmppserverv2

这是我展示Firebase云消息传递(FCM)XMPP连接服务器的示例Java项目。此服务器使用XMPP协议通过FCM CCS服务器将数据发送到客户端应用程序。

https://github.com/carlosCharz/fcmxmppserver

我还在youtube上创建了一个视频,在那里我解释了这些变化。

https://www.youtube.com/watch?v=KVKEj6PeLTc

希望你觉得它很有用。

以下是实施的代码段:

    public class CcsClient implements StanzaListener {

    //Other code

    public void connect() throws XMPPException, SmackException, IOException {
    XMPPTCPConnection.setUseStreamManagementResumptionDefault(true);
    XMPPTCPConnection.setUseStreamManagementDefault(true);

    XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();
    config.setServiceName("FCM XMPP Client Connection Server");
    config.setHost(Util.FCM_SERVER);
    config.setPort(Util.FCM_PORT);
    config.setSecurityMode(SecurityMode.ifpossible);
    config.setSendPresence(false);
    config.setSocketFactory(SSLSocketFactory.getDefault());
    // Launch a window with info about packets sent and received
    config.setDebuggerEnabled(mDebuggable);

    // Create the connection
    connection = new XMPPTCPConnection(config.build());

    // Connect
    connection.connect();

    // Enable automatic reconnection
    ReconnectionManager.getInstanceFor(connection).enableAutomaticReconnection();

    // Handle reconnection and connection errors
    connection.addConnectionListener(new ConnectionListener() {

        @Override
        public void reconnectionSuccessful() {
            logger.log(Level.INFO, "Reconnection successful ...");
            // TODO: handle the reconnecting successful
        }

        @Override
        public void reconnectionFailed(Exception e) {
            logger.log(Level.INFO, "Reconnection failed: ", e.getMessage());
            // TODO: handle the reconnection failed
        }

        @Override
        public void reconnectingIn(int seconds) {
            logger.log(Level.INFO, "Reconnecting in %d secs", seconds);
            // TODO: handle the reconnecting in
        }

        @Override
        public void connectionClosedOnError(Exception e) {
            logger.log(Level.INFO, "Connection closed on error");
            // TODO: handle the connection closed on error
        }

        @Override
        public void connectionClosed() {
            logger.log(Level.INFO, "Connection closed");
            // TODO: handle the connection closed
        }

        @Override
        public void authenticated(XMPPConnection arg0, boolean arg1) {
            logger.log(Level.INFO, "User authenticated");
            // TODO: handle the authentication
        }

        @Override
        public void connected(XMPPConnection arg0) {
            logger.log(Level.INFO, "Connection established");
            // TODO: handle the connection
        }
    });

    // Handle incoming packets (the class implements the StanzaListener)
    connection.addAsyncStanzaListener(this,
            stanza -> stanza.hasExtension(Util.FCM_ELEMENT_NAME, Util.FCM_NAMESPACE));

    // Log all outgoing packets
    connection.addPacketInterceptor(stanza -> logger.log(Level.INFO, "Sent: {}", stanza.toXML()), stanza -> true);

    connection.login(fcmServerUsername, mApiKey);
    logger.log(Level.INFO, "Logged in: " + fcmServerUsername);
}\

}