【问题标题】:Sending XMPP packets to Openfire plugin on Android将 XMPP 数据包发送到 Android 上的 Openfire 插件
【发布时间】:2012-08-08 01:35:05
【问题描述】:

我正在尝试实现一个简单的 Android 应用程序,该应用程序在为 Openfire 服务器编写的插件之间发送和接收数据包。该插件旨在从客户端接收数据包以进行进一步处理。所以这不是聊天。以下代码 sn -p 显示了我向服务器发送数据包的方式:

ConnectionConfiguration configuration = new ConnectionConfiguration(
        HOST, PORT);
Connection connection = new XMPPConnection(configuration);
try {
    connection.connect();
} catch (XMPPException e) {
    e.printStackTrace();
}
if (connection.isConnected()) {
    Packet packet = new Message();
    packet.setFrom("123456789@localhost");
    packet.setTo("987654321@component.localhost");
    connection.sendPacket(packet);
    connection.disconnect();
}

HOST 和 PORT 是预定义的常量。

我尝试在插件内的 if 子句中使用代码,它运行良好 - 组件接收数据包并与它们一起工作。但是,在我的 Android 应用程序中,此代码不起作用 - 数据包无法到达组件。

所以,伙计们,如果您有任何建议,我将非常感谢您的帮助。也许我在某处使用了错误的技术——我是 XMPP 和 Openfire 的新手。


更新

在应用程序的清单中有所有需要的权限。而HOST等于运行Openfire服务器的PC的静态IP地址。

private static final String HOST = "192.168.1.100";
private static final int PORT = 5222;

【问题讨论】:

  • 如果你不打电话给connection.login(),不知道这在电脑上如何工作。
  • 你知道这实际上是一个非常聪明的想法。但是,我使用了 connection.loginAnonymously()。谢谢你,Flow,你让我很开心。
  • 很高兴能帮上忙。如果这样做了,我还建议使用 iq 类型节而不是消息类型节。如果没有,请尝试启用 Smack debug facilities 和/或附加调试器以了解发生了什么。

标签: android xmpp send packet openfire


【解决方案1】:

为了向服务器发送数据包,您应该使用org.jivesoftware.smack.Connection 类的login()loginAnonymously() 方法登录到它。

谢谢先生。提示的流程。

【讨论】:

    【解决方案2】:

    连接和断开

    // 为这个新连接创建配置
    ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
    config.setCompressionEnabled(true);
    config.setSASLAuthenticationEnabled(true);
    
    连接连接 = 新 XMPPConnection(config);
    // 连接到服务器
    连接.连接();
    // 登录服务器
    connection.login("用户名", "密码", "SomeResource");
    ……
    // 断开与服务器的连接
    连接.断开();
    代码>
    

    你应该先登录,这里是管理连接的指南http://www.igniterealtime.org/builds/smack/docs/latest/documentation/connections.html

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多