如何在Smack中启用XEP-0199?

时间:2014-05-30 10:24:46

标签: android xmpp ping smack asmack

我正在使用aSmack。我的应用程序侦听聊天室并对消息作出反应,但它从不发送消息。如果聊天室保持静默一段时间,然后发送新消息,则应用程序不会收到更多消息。我研究过,我认为XEP-0199就是解决方案。我看到@Flow(当前的Smack维护者)implemented itthe issue related已关闭。

我认为我需要使用PingProvider,但我真的不知道如何将此课程与Connection相关联。

如何启用XEP-0199?我如何使用PingProvider


连接代码:

smack = SmackAndroid.init(getActivity().getApplicationContext());
connection = new XMPPConnection(App.getServer());
connection.addConnectionListener(new ConnectionListener() {

    private final static String SMACK = "SMACK";

    @Override
    public void reconnectionSuccessful() {
        Log.i(SMACK , "reconnectionSuccessful");
    }

    @Override
    public void reconnectionFailed(Exception e) {
        Log.i(SMACK, "reconnectionFailed", e);
    }

    @Override
    public void reconnectingIn(int seconds) {
        Log.i(SMACK, "reconnectingIn " + seconds);
    }

    @Override
    public void connectionClosedOnError(Exception e) {
        Log.i(SMACK, "connectionClosedOnError", e);
    }

    @Override
    public void connectionClosed() {
        Log.i(SMACK, "connectionClosed");
    }
});
connection.connect();
connection.login(user, password);

4 个答案:

答案 0 :(得分:6)

我修复了手动实现ping响应的问题:

connection.addPacketListener(new PacketListener() {

    @Override
    public void processPacket(Packet packet) {
        connection.sendPacket(new Pong((Ping) packet));
    }
}, new PacketFilter() {

    @Override
    public boolean accept(Packet packet) {
        return packet instanceof Ping;
    }
});

答案 1 :(得分:5)

防止用户断开会话

PingManager pm =  PingManager.getInstanceFor(MyApplication.connection) ;
    pm.setPingInterval(5) ;  // 5 sec
    pm.pingMyServer() ;
    pm.registerPingFailedListener(new PingFailedListener() {

        @Override
        public void pingFailed() {
            Log.e(TAG , "pingFailed") ;
        }
    });

答案 2 :(得分:1)

XEP 0199不是解决方案,Ping用于检查服务器启用的天气。实际上你会发送ping到服务器。

现在和你的问题一样紧张。显示您要发送的消息节。并检查聊天室是公共还是私人。你无法向私人聊天室发送消息。

答案已更新:

尝试使用此代码检测邮件接收

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        Network.connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message.getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]");
                   //recieve.setText(message.getBody());
                    /*messages.add(fromName + ":");
                    messages.add(message.getBody());*/
                    // Add the incoming message to the list view
                    item = new RowItem(R.drawable.billing, message.getBody());
                    adapter = new CustomListViewAdapter(getBaseContext(),
                            R.layout.list_item, rowItems);
                    rowItems.add(item);
                    //listView.setAdapter(adapter);
                }
            }
        }, filter);

答案 3 :(得分:0)

我调用了PingManager.getInstanceFor方法来启用XEP-0199支持。