gpt4 book ai didi

java - 如何在 android 中有效地使用持久性 XMPP

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:36:45 25 4
gpt4 key购买 nike

我想开发一个即时通讯应用程序。 GCM,用于推送数据是一种流行(且有效)的方式,如果你在 android 上,但由于以下原因我没有使用它:

  1. 它删除了 100 多条未送达的消息。
  2. 它不能在没有谷歌应用程序的设备上运行。

相反,我决定设置传统的 XMPP 服务器 (openFire),并使用 Smack api(TCP 连接)进行连接。到目前为止,进展顺利,但我有一些担忧。

这是我写的一个小测试代码(它运行在一个服务中):

    Log.d("TAG","service has started");
SmackConfiguration.setDefaultPacketReplyTimeout(10000);
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("admin", "football100")
.setServiceName("harsh-pc")
.setHost("192.168.0.200")
.setPort(5222).setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.build();

final AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
conn2.connect();

conn2.login();

Presence presence = new Presence(Presence.Type.available);
presence.setStatus("online");
// Send the packet (assume we have an XMPPConnection instance called "con").
conn2.sendStanza(presence);

} catch (SmackException | IOException | XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG", e.toString());
}
StanzaFilter filter=new StanzaFilter() {
@Override
public boolean accept(Stanza stanza) {
return true;
}
};
StanzaListener listener= new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
Log.d("TAG","recevied stuff");
ChatManager chatmanager = ChatManager.getInstanceFor(conn2);


Chat newChat = chatmanager.createChat("harsh@harsh-pc");
newChat.sendMessage("Reply :) ");
}
};

conn2.addAsyncStanzaListener(listener,filter);
ChatManager chatmanager = ChatManager.getInstanceFor(conn2);


Chat newChat = chatmanager.createChat("harsh@harsh-pc");

try {
Random r=new Random();

// newChat.sendMessage(Integer.toString(r.nextInt()));
Thread.sleep(1500);


}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG",e.toString());
}
}
}).start();


final Thread sleeper=new Thread(new Runnable() {
@Override
public void run() {
for(;;){
try {
Thread.sleep(100000);
Log.d("TAG","SLEEPI");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});sleeper.start();

请注意此代码段的最后一部分。 我必须运行一个无限循环,以便我可以继续监听传入的数据包(如果我不包含此代码,我将无法拦截任何传入的数据包)。

我的问题是:

  1. 这种方法会消耗大量电池吗?
  2. 此方法是否会阻止设备 hibernate ?
  3. 是否有更好的方法来完成任务(不使用 GCM)?
  4. 有没有办法将 GCM 与 OPENFIRE 集成?

最佳答案

我们过去有同样的要求,我们为我们的应用程序提供了外部聊天功能,为此我们使用了 XMPP [使用 aSmack],

很高兴知道您已经确定需要使用外部服务才能与服务器建立持久性 xmpp 连接,但要确保服务应该是后台服务并使其成为粘性服务。

使用 GCM 或 XMPP 实际上取决于您如何设计系统,您可以在您的应用程序中同时使用它们,以使其更稳定并导致更复杂!!!!!!

制作如此复杂的系统仍然很困难,您始终可以使用简单的可用解决方案,即 XMPP。

Is this method going to take tremendous amounts of battery? It should not!!! as you are using default ping interval here(which is actually 30 min),it should not consume more battery. if you had used ping interval less than 5 min then you should worried about battery drainage problem !!!

Will this method prevent the device from sleeping? As per my understanding ,You are using TCP connection here unless there is no data on input-stream device can go into the sleep ,as soon as some data arrive device will wake up and or you send some data on same socket [such like heartbeat/ping] to keep cpu awake .but still this behavior can vary as per different mobile manufacture.

Is there a better way to get things done (without using GCM) ? XMPP is always a strong alternative of gcm,as long as you know what are you really looking for.You can always try to optimize your system for batter output.

Is there a way to integrate GCM with OPENFIRE ? I am confused here what you really want to ask here !!! gcm(CCS) is google implementation of XMPP server you can always create a XMPP client using smack but to integrate GCM with Openfire means you are looking at server to server communication ,which is out of scope of you application requirements as per my understanding!!!!

下面是一些有用的链接,可以帮助您完成研究

Should applications using aSmack use foreground Services?

How to keep a XMPP connection stable on Android with (a)smack?

How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?

Keep XMPP connection (using asmack) alive on Android

关于java - 如何在 android 中有效地使用持久性 XMPP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com